0% found this document useful (0 votes)
20 views

Data Types in Java

Java Data Types

Uploaded by

ms.madhu27
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 views

Data Types in Java

Java Data Types

Uploaded by

ms.madhu27
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
You are on page 1/ 5

Core Java Madhusudhan

Data Types in Java:


• The Java programming language is a statically typed language, which means that every
variable and every expression has a type that is known at compile time.
• Data types specify the different sizes and values that can be stored in the variable.
• The types of the Java programming language are divided into two categories:
1. Primitive types and
2. Non-primitive (reference) types.

1. Primitive Data Types


• The primitive types are the boolean type and the numeric types.
• The Java programming language supports eight primitive data types.
• The numeric types are the integral types byte, short, int, long, and char, and the
floating-point types float and double.
• A primitive type is predefined by the language and is named by a reserved keyword.
• Primitive values do not share state with other primitive values.

Byte Data Type:


• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Byte data type is used to save space in large arrays, mainly in place of integers, since
a byte is four times smaller than an integer.

Short Data Type:


• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2 times
smaller than an integer

Int Data Type:


• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there is a
concern about memory.

Long Data Type:


• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed

Float Data Type:


• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers

Cell: 78 42 66 47 66 https://www.linkedin.com/in/madhu-baswani/
Core Java Madhusudhan
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f

Double Data Type:


• double data type is a double-precision 64-bit IEEE 754 floating point
• This data type is generally used as the default data type for decimal values, generally
the default choice
• Double data type should never be used for precise values such as currency

Boolean Data Type:


• Boolean data type represents one bit of information
• There are only two possible values: true and false.
• This data type is used for simple flags that track true/false conditions.
• This data type represents one bit of information, but its "size" isn't something that's
precisely defined.

Char Data Type:


• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character

Examples of Integral Data Types:


class Test {
public static void main(String[] args) {
byte x1 = 127;
short x2 = 3276;
int x3 = 21648;
long x4 = 2147483648l;
long x5 = 2147483648L;

System.out.println("Byte :" + x1);


System.out.println("Short :" + x2);
System.out.println("Int :" + x3);
System.out.println("Long :" + x4);
}
}

Examples of Floating-point Data Types:


class Test {
public static void main(String[] args) {
float x = 1.2F;
double y = 2.45;
double z = 2.45d;

System.out.println("Float : " + x);


System.out.println("Double : " + y);
System.out.println("Double : " + z);
}
}

Cell: 78 42 66 47 66 https://www.linkedin.com/in/madhu-baswani/
Core Java Madhusudhan
Examples of boolean Data Types:
class Test {
public static void main(String[] args) {
boolean x = true;
boolean y = false;

System.out.println("Boolean : " + x);


System.out.println("Boolean : " + y);
}
}

Examples of char Data Types:


Example 1:
class Test {
public static void main(String[] args) {
char x = 'M';
char y = 'L';

System.out.println("Char : " + x);


System.out.println("Char : " + y);
}
}

Example: 2
class Test {
public static void main(String[] args) {
char x = '\u0041';
char y = '\uffff';

System.out.println("Char : " + x);


System.out.println("Char : " + y);
}
}

2. Non-Primitive or Reference Data Types:


• The reference types are class types, interface types, and array types.
• There is also a special null type.
• An object is a dynamically created instance of a class type or a dynamically created
array.
• The values of a reference type are references to objects. All objects, including arrays,
support the methods of class Object.
• A reference variable can be used to refer any object of the declared type or any
compatible type.
• Example:
o Employee e = new Employee();
o Customer c = new Customer();

Examples of Non-primitive Data Types:


class Test {
public static void main(String[] args) {
String s = new String();
Test t1 = new Test();
Cell: 78 42 66 47 66 https://www.linkedin.com/in/madhu-baswani/
Core Java Madhusudhan
Test t2 = new Test();
Test t3 = new Test();
A a = new A();

System.out.println("String : " + s);


System.out.println("Test : " + t1);
System.out.println("Test : " + t2);
System.out.println("Test : " + t3);
System.out.println("A : " + a);
}
}

class A {
}

Output:
String :
Test : Test@15db9742
Test : Test@6d06d69c
Test : Test@7852e922
A : A@4e25154f

Default Values for data types:


• Primitive
o Integrals : 0
o Floating-point : 0.0
o Char : '\u0000' (Single Space)
o Boolean : false
• Non-primitive
o For all non-primitive : null

Example to print default values:


class Test {
static byte x1;
static short x2;
static int x3;
static long x4;

static float x5;


static double x6;

static char x7;


static boolean x8;

static String x9;


static Test x10;

public static void main(String[] args) {


System.out.println("Byte : " + x1);
System.out.println("Short : " + x2);
System.out.println("Int : " + x3);
System.out.println("Long : " + x4);
System.out.println("Float : " + x5);
System.out.println("Double : " + x6);
Cell: 78 42 66 47 66 https://www.linkedin.com/in/madhu-baswani/
Core Java Madhusudhan
System.out.println("Char : " + 'A' + x7 + 'B');
System.out.println("Boolean : " + x8);
System.out.println("String : " + x9);
System.out.println("Test : " + x10);
}
}

Wrapper Class for Primitive:


Primitive Default value Wrapper Class
Data Types (for fields) (java.lang)
byte 0 Byte
short 0 Short
int 0 Integer
long 0L Long
float 0.0f Float
double 0.0d Double
boolean false Boolean
char '\u0000'(single space) Character

Note (for local variables in Java):


• Local variables are slightly different; the compiler never assigns a default value to an
uninitialized local variable.
• If we cannot initialize our local variable where it is declared, make sure to assign it a
value before we attempt to use it.
• Accessing an uninitialized local variable will result in a compile-time error.

Unicode System
UNICODE: Universal International Standard Character Encoding.
Before UNICODE:
• ASCII
• ISO

Least Value: '\u0000'


Max Value: '\uffff'

Cell: 78 42 66 47 66 https://www.linkedin.com/in/madhu-baswani/

You might also like