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.2Non-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 {
<