
Core Java
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
All prices
More details
Content
CHAPTER 2 - Language Fundamentals
2.1 Data Types and Variables
2.1.1 Primitive Data Types
In Java, primitive data types are the basic building blocks for defining and storing simple values. They represent fundamental data types and are used to store numeric values, characters, and boolean values. Java has eight primitive data types, categorized into four groups: integers, floating-point numbers, characters, and Booleans.
Here are the eight primitive data types in Java:
1. Integers:
byte = 8-bit signed integer. Range: -128 to 127.
short = 16-bit signed integer. Range: -32,768 to 32,767.
int = 32-bit signed integer. Range: -2^31 to (2^31 - 1).
long = 64-bit signed integer. Range: -2^63 to (2^63 - 1).
Syntax
byte b = 127;
short s = 32767;
int integer = 2147483647;
long l = 9223372036854775807L; // Note the 'L' suffix for long literals.
2. Floating-Point Numbers:
float = 32-bit IEEE 754 floating-point number. Approximate range: ±3.4 x 10^38 with limited precision.
Double = 64-bit IEEE 754 floating-point number. Approximate range: ±1.7 x 10^308 with higher precision.
Syntax
float f = 3.14f; // Note the 'f' suffix for float literals
double d = 3.14159265359;
3. Characters:
char = 6-bit Unicode character. Represents a single character in the Unicode standard.
Syntax
char c = 'A';
4. Booleans:
Boolean = Represents a true or false value.
Syntax
boolean isprogrammingFun = true;
boolean isProgrammingHard = false;
2.1.2 Non-Primitive Data Types
Non-Primitive Data Types user-defined data types created by users. These data types are used to store multiple values. When non-primitive data is defined, it refers to the memory location in the heap memory where the data is stored, i.e. the memory location where the object is located. For this reason, non-primitive data type variables are also called reference data types or object-only reference variables.There are five types of non-primitive data types in Java:
Class and Object - These are user_defined data types that you create by defining classes. When you create an instance(object)of a class, a reference variable is used to store the memory address of that object.
Syntax-
Class MyClass {
// Class definition
}
MyClass obj = new MyClass();
String- A string represents a sequence of characters for example "CoreJava", "Hello world", etc. String is the class of Java.
Syntax- String str = "Core Java Book";
Array - Arrays in Java are reference data types. They allow you to store multiple values of the same data type in a single data structure. An array variable stores the reference to the array object in memory.
Syntax -int[] num = new int[50]; // 'num' is a reference to an array of integers
Interface - Interfaces are reference data types that define a contract for classes to implement. You can use interface reference variables to store objects of any class that implements the interface.
Syntax-
interface MynewInterface {
void myMethod();
}
class MyImplementation implements MynewInterface {
public void myMethod() {
// Implementation
}
}
MynewInterface interfaceRef = new MyImplementation(); // 'interfaceRef' is a reference to an object of MyImplementation
2.1.3 "char" data type
The char data type is 16 bits, which means it uses 2 bytes of memory to represent a character. This gives the char data type a range of 65,536 different Unicode characters, allowing it to represent characters from a wide range of languages and symbol sets.
The size of the char data type in Java (2 bytes) differs from C and C++ primarily due to the historical differences in the design and goals of these programming languages:
Unicode Support: Java was designed from the beginning to be an international and platform-independent language. As a result, it uses Unicode to represent characters, which requires a larger range of values than the ASCII character set used in C and C++. Unicode is capable of representing characters from a wide variety of languages, including non-Latin scripts, symbols, and special characters, making it more suitable for global applications.
Platform Independence: Java was designed with the goal of "Write Once, Run Anywhere" (WORA). To achieve this, Java made the decision to use a fixed-width 16-bit char type, ensuring consistency in character representation across different platforms and avoiding the platform-dependent character encoding issues that C and C++ can face.
Avoiding Character Encoding Ambiguity: In C and C++, the size of the char data type can vary from one platform to another, leading to potential portability issues when dealing with character encodings. By using a fixed-width 16-bit char, Java avoids such ambiguity and simplifies character handling.
While Java's choice of a 16-bit char data type is generally suitable for its goals of platform independence and internationalization, it does mean that some characters, particularly those outside the Basic Multilingual Plane (BMP) of Unicode, require a pair of char values (a surrogate pair) to represent them fully. This is a trade-off made to ensure broader character support and portability.
2.2 Declaring and Initializing Variables-
1. Declaration: To declare a variable in Java, specify its data type followed by the variable name. For example:
int salary; // Declaring an integer variable named "salary"
double price; // Declaring a double variable named "price"
String name; // Declaring a String variable named "name"
2. Initialization: Initialization is the process of giving a variable its initial value. After declaring a variable, you can initialize it by assigning a value. For example:
salary = 25000; // Initializing "salary" with the value 25000
price = 19.99; // Initializing "price" with the value 19.99
name = "Jay"; // Initializing "name" with the string "Jay"
You have to remember some rules while giving variable names and they are as follows:
Variable names are case-sensitive so "myFunction" and "myfunction" are different.
Variable names can contain letters, digits, underscores, and dollar signs but cannot start with a digit.
Java has reserved words (keywords) that cannot be used as variable names.
2.2.1 Types of Variables
In Java, variables are categorized into different types based on their scope, lifetime, and where they are declared. Here are the main types of variables in Java:
Local Variables:
Local variables are declared within a method, constructor, or block of code.
They have limited scope, which means they are accessible only within the method, constructor, or block in which they are declared.
Local variables are created when the method is called and destroyed when the method exits.
Example-
public void exampleMethod() {
int localVar = 10; // Local variable
// ...
}
Instance Variables (Non-Static Fields):
Instance variables are declared within a class but outside any method, constructor, or block.
They are associated with an instance (object) of the class and have different values for each instance.
Instance variables have default values if not explicitly initialized.
Example-
public class MyClass {
int instanceVar; // Instance variable
}
Static Variables (Class Variables):
Static variables are declared with the static keyword within a class but outside any method, constructor, or block.
They belong to the class itself rather than to any particular instance of the class.
There is only one copy of a static variable shared among all instances of the class.
Example-
public class MyClass {
static int staticVar; // Static variable
}
Final Variables:
Final variables are declared with the final keyword.
They can be assigned a value only once and cannot be modified thereafter.
Final variables can be used for constants or to ensure that a variable's value remains constant.
Example-
final int myConstant = 42; // Final variable
Parameter Variables:
Parameter variables are declared within a method's parameter list.
They receive values when the method is called and can be used within the method.
They act like local variables within the method but are initialized with values provided as arguments.
Example-
public void exampleMethod(int parameterVar) {
// parameterVar acts as a parameter variable
// ...
}
Array Elements:
Elements of an array are also variables.
Each element can hold a value of the same data type as the array.
Array elements are accessed using an index.
Example-
int[] numbers = {1, 2, 3, 4, 5}; // Array elements
int thirdNumber = numbers[2]; // Accessing an array element
These are the primary types of variables in Java. Understanding their characteristics and scope is essential for writing effective and efficient Java...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.