Site icon Software Testing

Data Types used in Java

Data Types used in Java

Data Types used in Java

Data Types used in Java, What is Data Type?, Primitive Data Types, Non-primitive Data Types, and Data type conversion in Java programming.

Selenium Online Training with Project (by G C Reddy)

Data Types are used in computer programming to store and organise different types of data.

Data Types used in Java

1. Data Type is a classification of the type of data that a variable or final variable/constant or method can store in a computer program

Data types are used  in four area in Java Programs.

a. Declaring Variables
b. Declaring Final Variables/Constant
c. Declaring Methods (methods with return a value)
d. Method Arguments

Example:

1.
int x=100;
String country=”India”;

2.
public int add(int num1, int num2) {
int result;
result=num1+num2;
return result;
}

3.
(int num1, int num2)

4.
final int price=1000;

2. Data Types specify sizes and types that can be stored in a variable.

byte a=100;
short b=10000;
int c=100000;
long d=9898787876l;

String x=”Hyderabad”;
char y=’R’;
boolean z= true;

3. Java Supports Explicit Declaration of data types

Java:

int x;
x=100;
x=10.23; //Invalid
y=200;

Python:

x=100
.
.
x=”India”
.
.
x=10.234

4. Java Data Type Conversion

Assign Values to Variables is two types
1. Initialisation
int x=100;
int y;
.
.
y=1234;

2. Reading
Read User Input
Read data from files

Whenever we read data in a computer program then the program considers the data as String type data, we need convert the data to perform mathematical operations.

India – > “India” – No conversion
100 -> “100” – Convert the String type data to integer type
10.234 – > “10.234” – Convert the String type data to float type

Ex:
String num1=”10″;
String num2=”20″;

System.out.println(num1+num2);//1020
int x= Integer.parseInt(num1);
int y= Integer.parseInt(num2);
System.out.println(x+y);//20

5. Important Java Types

a. Primitive Data Types

i. byte
Ex: byte a=100;

ii. short
Ex: short b=1000;

iii. int
Ex: int c=10000000;

iv. long
Ex: long d=998787879l;

v. float
Ex: float e=123.456f;

vi. duble
duble f= 8787878.98345;

vii. char
Ex: char g=’Y’;

viii. booean
booean h=false;

b. Non-primitive Data Types

i. String

ii,. Array

iii. ArrayList

iv. Object

Etc.

Note:

Java Data Types Tutorial

Java Language Syllabus

Follow me on social media: