Java Program Example, Comments in Java Programs and Java Data Types

preview_player
Показать описание
Java Programming example with methods declaration, Variables declaration, conditions, Loops, and other programming features.
Adding comments in Java programs, Data types in Java Programming, primitive data types and non-primitive data types.
Рекомендации по теме
Комментарии
Автор

Class Notes:
Java Tutorial 2: Java Program Example, Comments in Java, Java Data Types

i) Java Program Example

ii) Comments in Java Programs

iii) Java Data Types

i) Java Program Example

User Defined Methods
1) Non Static Methods (Method with returning value, Method without returning any value)
We call non static methods using Object /Instance of the Class

2) Static Methods (Method with returning value, Method without returning any value)
We call Static methods using Class name.

Data Types
We use Data types for declaring Variables, Constants and Methods with retuning value

//It is a Sample Java Program to understand Java Syntax and Java Program Structure
package abcd;

public class Sample {
//Create a Methods with Parameters an Returning value.(Non-Static Method)
public int add(int a, int b){
int result;
result = a + b;
return result;
}
//Create a Method with no Parameters and Returns nothing(Non-Static Method)
public void comparison(){
int a =100, b=50;

if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
}
//Create a Method with Parameters, returning a value(Static Method)
public static int sub(int a, int b){
int result = a-b;
return result;
}
//Create a Method with no parameters and returns nothing (Static Method)
public static void comparison2(){
int a =100, b=500;

if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
}
public static void main(String[] args) {
int a; //Variable Declaration
a =10; //Initialization
int b=20; //Variable declaration and initialization
int d, e, f;//Declaring multiple variables
int g=1, h=2, k=3; //Declaring multiple variables and Initialization

double c =12.345;
char l = 'Z';
boolean x = true;
String y = "Selenium Testing";

Testing

int m = a+b;
System.out.println(m);//30
System.out.println(l);
System.out.println(x);//true

final int abc = 100; //Integer type Constant Declaration
System.out.println(abc);//100

if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}

char grade = 'a';

switch (grade){ //decide among several alternates

case 'A':

break;

case 'B':
System.out.println("Good");
break;

case 'C':

break;

default:
System.out.println("Invalid Grade");
}

for (int i = 1; i <= 5; i++){
if (i != 4){
System.out.println(i);
}
}

int j =1;
while (j <= 10){
if ((j != 4) && (j != 7)){
System.out.println(j);
}
j++;
}

int n=10;
do
{
System.out.println(n);
n++;
} while (n <= 5);

String [] tools = {"Selenium", "UFT", "RFT", "SilkTest"};


for (String xyz: tools){
System.out.println(xyz);
}
//Create Object and Call Non-static Methods

Sample obj = new Sample();
int res = obj.add(12, 45);
System.out.println(res);

System.out.println(obj.add(12, 45));

obj.comparison();

//Call Static Methods
int res2= Sample.sub(18, 11);
System.out.println(res2);

System.out.println(Sample.sub(18, 11));

Sample.comparison2();
}
}

ii) Comments in Java Programs

Comments are English words used for Code documentation

1) Purpose
a) To make the code readable
b) To make the code disable from execution

2) Comments Syntax in Java

a) Use // for Single line Comment

b) Use


for multiple line comment

Or
Comment multiple lines (Eclipse IDE)

Select Statements / steps
Source menu > Add block comment

Uncomment
Select Comment block
Source menu > Remove block Comment

3) Example:

public class Class2 {
//It is a Sample Java Program
public static void main(String[] args) {
int a, b; //Variables Declaration
a =10; b=20;
// Display Addition of a, b
System.out.println("Addition of a, b is " + (a+b));

/*if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}*/
}
}

4) Usage of Comments in Test Automation

a) To write Test case Header

b) To write methods header

c) To explain complex logic
etc...

iii) Java Data Types

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 program.

Ex:
General: Alpha bytes, Numeric, Special Characters, Alpha-numeric etc...

Computer Programming: character, integer, double, Boolean, String etc...

Java supports Explicit declaration of Data Types.

(We need specify the data type before declaring variables, constants and methods with returning value)

Variables

Syntax and Example:
1)
dataType variableName;
int a;

2)
dataType variablename = value;
int a =100;

3)
dataType variable1Name, variable2Name, variable3Name;
int a, b, c;

4)
dataType variable1Name=value, variable2Name=value, variable3Name=value;
int a=10, b=20, c=30;

Constant

nonAcessModifier dataType constantName = value;
final int price = 100;

Method

accessModifier dataType methodName (Parameters){
Statements


}

public int add(int a, int b){
int result = a+b;
return result;
}

Two Types of Data Types in Java,

a) Primitive Data Types

b) Non-primitive Data Types / Reference data types

a) Primitive Data Types (8 data types)

1) Integer Data Types

i) byte (8 bits)
Ex: byte a = 10;

ii) short (16 bits)
Ex: short b=173;

iii) int (32 bits)
ex: int c =1000;

iv) long (64 bits)
Ex: long

2) Relational Types (Numbers with decimal places)

v) float (32 bits)

float x =1.23;

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

3) Characters

vii) char
char z ='A';
char x ='1';

4) Conditional

viii) boolean
boolean x = true;

b) Non-primitive Data Types

Non-primitive Data Types or Refrence Data Types in Java are Objects and Arrays.

Synatx and example

ClassName objectName = new Classname();

Sample abc = new Sample();

Convert Data from one type to another

Two types of value assignment for Variables,

1) Initialization

int a =100; //Initialization

or
int b;
b=200;

2) Reading

a) Read data using Input devices
b) Read from files etc...

Note: if you read data (any type data) then computer program considers the data as String type data,
in order to perform mathematical operations then we need to convert the data.

Note 2: We can't convert Alpha bytes to Integers or Double values...

gcreddy
join shbcf.ru