Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Java Notes

Java has 8 primitive data types: boolean, byte, short, int, long, float, double, and char. Each data type stores different types of values and has different ranges. Variables are containers that store values of a specific data type. Variable names must follow certain rules like not starting with a number and being case sensitive. Java also supports non-primitive data types like classes, arrays, and interfaces that can store more complex data.

Uploaded by

masomeen31
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Notes

Java has 8 primitive data types: boolean, byte, short, int, long, float, double, and char. Each data type stores different types of values and has different ranges. Variables are containers that store values of a specific data type. Variable names must follow certain rules like not starting with a number and being case sensitive. Java also supports non-primitive data types like classes, arrays, and interfaces that can store more complex data.

Uploaded by

masomeen31
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Notes

Variables
 A variable is a container that stores a value.
 This value can be changed during the execution of the program.
 Example: int number = 8; (Here, int is a data type, the number is the variable
name, and 8 is the value it contains/stores).
Rules for declaring a variable name
We can choose a name while declaring a Java variable if the following rules are
followed:
 Must not begin with a digit. (E.g., 1arry is an invalid variable)
 Name is case sensitive. (Harry and harry are different)
 Should not be a keyword (like Void).
 White space is not allowed. (int Code With Harry is invalid)
 Can contain alphabets, $character, _character, and digits if the other
conditions are met.

What is Data Types in Java?


Data Types in Java are defined as specifiers that allocate different sizes
and types of values that can be stored in the variable or an identifier. Java
has a rich set of data types. Data types in Java can be divided into two
parts :
1. Primitive Data Types :- which include integer, character, boolean,
and float
2. Non-primitive Data Types :- which include classes, arrays and
interfaces.
Primitive Data Types
Primitive Data Types are predefined and available within the Java
language. Primitive values do not share state with other primitive values.
There are 8 primitive types: byte, short, int, long, char, float, double, and
boolean
1. boolean type
 The boolean data type has two possible values, either true or false.
 Default value: false
 They are usually used for true/false conditions.

class Main {

public static void main(String[] args) {

boolean flag = true;

System.out.println(flag); // prints true

2. byte type
 The byte data type can have values from -128 to 127 (8-bit signed two's
complement integer).
 If it's certain that the value of a variable will be within -128 to 127, then it
is used instead of int to save memory.

 Default value: 0

class Main {

public static void main(String[] args) {


byte range;

range = 124;

System.out.println(range); // prints 124

3. short type

 The short data type in Java can have values from -32768 to 32767 (16-
bit signed two's complement integer).
 If it's certain that the value of a variable will be within -32768 and 32767,
then it is used instead of other integer data types (int, long)

class Main {

public static void main(String[] args) {

short temperature;

temperature = -200;

System.out.println(temperature); // prints -200


}

4. int type
 The int data type can have values from -231 to 231-1 (32-bit signed two's
complement integer).
 If you are using Java 8 or later, you can use an unsigned 32-bit integer.
This will have a minimum value of 0 and a maximum value of 232-1.

 Default value: 0

class M ain {

public static void main(String[] args) {

int range = -4250000;

System.out.println(range); // print -4250000

5. long type
 The long data type can have values from -263 to 263-1 (64-bit signed two's
complement integer).
 If you are using Java 8 or later, you can use an unsigned 64-bit integer
with a minimum value of 0 and a maximum value of 264-1.

 Default value: 0
class L o n g E x am p le {

public static void main(String[] args) {

long range = -42332200000L;

System.out.println(range); // prints -42332200000

}
Notice, the use of L at the end of -42332200000. This represents that it's an
integer of the long type.

6. double type

 The double data type is a double-precision 64-bit floating-point.


 It should never be used for precise values such as currency.

 Default value: 0.0 (0.0d)

class M ain {

public static void main(String[] args) {

double number = -42.3;

System.out.println(number); // prints -42.3

}
}

7. float type

 The float data type is a single-precision 32-bit floating-point.


 It should never be used for precise values such as currency.

 Default value: 0.0 (0.0f)

class M ain {

public static void main(String[] args) {

float number = -42.9f;

System.out.println(number); // prints -42.3

Notice that we have used -42.3f instead of -42.3 in the above program. It's
because -42.3 is a double literal.
To tell the compiler to treat -42.3 as float rather than double , you need to
use f or F .

8. char type
 It's a 16-bit Unicode character.
 The minimum value of the char data type is '\u0000' (0) and the
maximum value of the is '\uffff' .

 Default value: '\u0000'

class M ain {

public static void main(String[] args) {

ch ar letter = '\u 0051';

System.out.println(letter); // prints Q

Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.


Here is another example:

class Main {
public static void main(String[] args) {

char letter1 = '9';


System.out.println(letter1); // prints 9

char letter2 = 65;


System.out.println(letter2); // prints A

}
}
Here, we have assigned 9 as a character (specified by single quotes) to
the letter1 variable. However, the letter2 variable is assigned 65 as an integer
number (no single quotes).

You might also like