Data Types in Java

Data Types in Java, Primitive Data Types, Non-primitive Data Types, Java Strings, Java Numbers, Java Characters, and Java Boolean Data Type.

Java Tutorial for Beginners

Java Data Types – Java for Selenium

What is Data Type?

Data Type is a classification of the type of data that a Variable or Constant or Method can hold in computer programming

Example:

Character, Number, Number with decimal values, Logical data, String, Date, Currency etc…

Note: Java supports explicit declaration of Data Types.

(We need to specify the Data Type before declaring Variables, Constants, and methods…)

Chatcter (char)
chat s=’Z’;

Number (byte, short, int, long, float, double)

String

String a= “X”;
String b=”Selenium”,
String c=”Automated Testing using Selenium”,

Logical data (boolean),
Date
etc,

Syntax:

a. Variables

dataType variableName;
Or
dataType variableName = value;
Or
dataType variable1Name, variable2Name, variable3Name;
or
dataType variable1Name=value, variable2Name= value, variable3Name=value;

Example:

int a;
a=10;
int b=100;
int c, d, e;
c=30; d=40; e=50;
int f=60, g=70, h=80;

b). Constants

Note: Variables and Constants are for holding the data, Variables may vary, but constants never change

int a;
a=100;//Correct for Variable
.
.
.
.
a=30; //Correct for Variable
……………….
final int b;
b=200; //Incorrect
final int c =300;//Correct
.
.
.
c=400; //Incorrect

c. Method with Return Value

public int add(){ //Return type is int (Non Static Method)
.
.
return…;
}
……………….
public static int multiply(){ //Return type is int (Static Method)
.
.
return…;
}
……………….
public void sub(){ //No return type (Non Static Method)
.
.
}
……………….
public static void divison(){ //No return type (Static Method)
.
.
}

Two Categories of Data Types in Java

1) Primitive Data Types
2) Non-primitive data Types

1. Primitive Data Types

a) Integer Data Types

i) byte (8 bits)

  • Minimum value is -128 (-2^7),
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0

Example:

byte a=100;
int x=100;
byte b=-129; //Out of Range
byte c=127;
byte d=128;//Out of Range

ii) short (16 bits)

  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Default value is 0.

Example:
short b=1000;

iii) int (32 bits)

  • Minimum value is – 2,147,483,648 (-2^31)
  • Maximum value is 2,147,483,647(inclusive) (2^31 -1)
  • The default value is 0

Example:

int a=1;
int b=1000;
int c= 100000000;
long d=9878787878l;//Out of Range
System.out.println(d);

iv) long (64 bits)

  • 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)
  • Default value is 0L

Example:

long x=1000;
long y=999999999;
long z=3333333333l;

b) Relational Data Types (Numbers with Decimal Places)

v) float (32 bits)

float x = 10.23f;

vi) double (64 bits)
double y = 1234.5678765;

c) Character Data Type

vii) character

char z = ‘A’;
char s = ‘1’;
char r = ‘*’;

d) Conditional Data Type

viii) boolean

boolean k = true/false;

2. Non Primitive data types / Reference data types

Non Primitive data types in Java are Objects (String, Array etc…)

Example:

int a=100;
String b=”100″;
JavaExamples c = new JavaExamples();
……………….
123 – Integer Data Type
‘Y’ -Character
123.34 – float/double
“Selenium Testing” – String
“abc123*&^” – String
“123” – String
1 – Integer
‘1’ – Character

Converting Data / Data Conversion

Whenever we read data then the computer program considers that data as String type data, we need to convert the data in order to perform mathematical operations, Note: Generally we convert String type to Integer Type/ Relational type, we can’t convert Alpha bytes to numbers, etc…

String to Integer

String x=”100″

int num= Integer.parseInt(x);

String to Double

String y=”10.234″;

String num = Double.parseDouble(y);


Selenium Tutorial for Beginners

0. Selenium Tutorial – 0. Prerequisites to Learn Selenium
1. Selenium Tutorial – I. Introduction – 1. Selenium Syllabus
2. Selenium Tutorial – I. Introduction – 2. An Overview of Selenium
3. Selenium Tutorial – II. Java for Selenium – 1. Introduction to Java
4. Selenium Tutorial – II. Java for Selenium – 2. An overview of Java
5. Selenium Tutorial – II. Java for Selenium – 3. Java Environment Setup
6. Selenium Tutorial – II. Java for Selenium – 4. Java Keywords and Identifiers
7. Selenium Tutorial – II. Java for Selenium – 5. Java Basic Syntax
8. Selenium Tutorial – II. Java for Selenium – 6. Java Program Structure
9. Selenium Tutorial – II. Java for Selenium – 7. Java Comments
Follow me on social media: