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

5.core Java Data Types

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

5.core Java Data Types

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Leela Soft Core Java (J2SE) Madhusudhan

Data Types:
✓ In java every variable has a type.
✓ Every expression has a type and all types are strictly defined.

Types of Data Types:


1. Primitive Data Types
2. Non-primitive Data Types

Integer Data Types:


• byte, short, int, long

class Test {
public static void main(String[] args) {
byte x1 = 128; // error: incompatible types: possible lossy
conversion from int to byte
short x2 = 32768;// Test.java:6: error: incompatible types:
possible lossy conversion from int to
// short
int x3 = 2147483648;// error: integer number too large:
2147483648
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);
}
}

7/26/2021 5:00 PM
Floating Types:
• Float and Double
Ex:
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);
}
}

Boolean:

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

• true, false

class Test {
public static void main(String[] args) {
boolean x = true;
boolean y = false;

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


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

Character Type:
• char

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

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


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

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

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


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

Day: 7/27/2021 5:10 PM


Non-primitive:
• String, StringBuffer, Test, Customer, Employee

Ex:
class Test {
public static void main(String[] args) {
String s = new String();
Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
A a = new A();

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

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 {
}

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

Default Values:
• Primitive
• Integers : 0
• Floats : 0.0
• Character : (Single Space)
• Boolean : false
• Non-primitive
• For all non-primitive : null

Example:
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);

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

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);
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 Wrapper Class (java.lang)


byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Type Promotion:
class Test {
static byte x1 = 127;
static short x2 = x1;
static int x3 = x2;
static long x4 = x3;

static float x5 = x4;


static double x6 = x5;

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);
}
}

Ex:
class Test {

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

static char x1 = 'B';


static int x2 = x1;

public static void main(String[] args) {


System.out.println("Char : " + x1);
System.out.println("Int : " + x2);
}
}

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

Least Value: '\u0000'


Max Value: '\uffff'

[email protected] Cell: 78 42 66 47 66

You might also like