0% found this document useful (0 votes)
20 views8 pages

Data Types in Java

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types. It details eight primitive data types: boolean, byte, short, int, long, double, float, and char, along with their ranges and default values. Additionally, it introduces the non-primitive String type, explaining that strings are objects in Java, not primitive types.

Uploaded by

kartikeytrashed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Data Types in Java

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types. It details eight primitive data types: boolean, byte, short, int, long, double, float, and char, along with their ranges and default values. Additionally, it introduces the non-primitive String type, explaining that strings are objects in Java, not primitive types.

Uploaded by

kartikeytrashed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming

[Link]

Data Types in Java

As the name suggests, data types specify the type of data that can be stored
inside variables in Java.
Java is a statically-typed language. This means that all variables must be
declared before they can be used.

int topperworld;

Here, topperworld is a variable, and the data type of the variable is int.

The int data type determines that the topperworld variable can only contain
integers.

Java has two categories in which data types are segregated:

 Primitive Data Type: such as boolean, char, int, short, byte, long,
float, and double
 Non-Primitive Data Type or Object Data type: such as String,
Array, etc.
Java Programming

-> Primitive Data Types

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.

Example : Java boolean data type

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

boolean flag = true;


[Link](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

Example : Java byte data type

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

byte range;
Java Programming

range = 124;
[Link](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).
 Default value: 0

Example : Java short data type

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

short temperature;
temperature = -200;
[Link](temperature); // prints -200
}
}
Java Programming

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

Example : Java int data type

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

int range = -4250000;


[Link](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

Example : Java long data type

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

long range = -42332200000L;


[Link](range); // prints -42332200000
Java Programming

}
}

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)

Example: Java double data type

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

double number = -42.3;


[Link](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)


Java Programming

Example : Java float data type

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

float number = -42.3f;


[Link](number); // prints -42.3
}
}

Notice that we have used -42.3f instead of -42.3in 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'

Example: Java char data type

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

char letter = '\u0051';


[Link](letter); // prints Q
}
}
Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.
Java Programming

Here is another example:

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

char letter1 = '9';


[Link](letter1); // prints 9

char letter2 = 65;


[Link](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).
Hence, A is printed to the output. It is because Java treats characters as an

integer and the ASCII value of A is 65.


Java Programming

 Non-Primitive Data Types

String type

Java also provides support for character strings via [Link] class.
Strings in Java are not primitive types. Instead, they are objects. For
example,

String myString = "Java Programming";

Here, myString is an object of the String class.

Example: Create a String in Java


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

// create strings
String first = "Java";
String second = "Python";
String third = "JavaScript";

// print strings
[Link](first); // print Java
[Link](second); // print Python
[Link](third); // print JavaScript
}
}

In the above example, we have created three strings named first, second,

and third.

You might also like