Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Core Java Class Notes

The document provides an overview of variables, data types, operators, and user input in Java programming. It explains the concept of variables, their types (local and global), naming conventions, and the use of the final keyword for constants. Additionally, it covers various data types, operators (arithmetic, relational, logical, etc.), and methods for obtaining user input using Scanner and BufferedReader classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Core Java Class Notes

The document provides an overview of variables, data types, operators, and user input in Java programming. It explains the concept of variables, their types (local and global), naming conventions, and the use of the final keyword for constants. Additionally, it covers various data types, operators (arithmetic, relational, logical, etc.), and methods for obtaining user input using Scanner and BufferedReader classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Date:- 20/09/2022

 what is variable?
* variable is a box , container or holder, which will hold value inside it.
* suppose we have x = 10, y =20 then how much x + y?
* here in this question we have 10 and 20 values, and x, y are variables in math or
programming we cannot directly work with values ,we need variables
int x = 10;
int y = 20;
String name = "ram"
float salary = 1000.230

* inside memeory where data or value is store that location name called variable
* variable has two types
1. local variable
variable under { } called local
2. global variable
variable outside { } called global, its a class level
variable, or instance variable
---------------------------------------------------------------------------------------------------------------
public class Test {

//global or class level or instance variable


static int p = 20;

public static void main(String[] args) {

//local variable
int x = 20, y = 10;//inline variable

System.out.println(x + y);

{
//local variable
int a = 100;
System.out.println(x);
System.out.println(y);
System.out.println(a);
}
//System.out.println(a);
System.out.println(p);
}
}
=================================================================
=

 Rules or naming conversion of variable


* 1. Variable name shoud be start with _ underscore or character only.
* 2. Do not use space between variable names.
* 3. Variable name do not start with numbers, symbols.
* 4. Variable name must be meaningful, short, and simple.

int mycurrentaddress;
int myCurrentAddress;//camelcase
int my_current_address

* simple variable value can be modify


int x = 10;
x = 20;
----------------------------------------------------------------------------------------------------------------
public class Test {

public static void main(String[] args) {

//variable creation - variable initialzation or variable


//declration
int x = 20;

//variable re-initialization
x = 30;

//variable declare
int y;

//assign value to variable


y = 200;

System.out.println(x);
}
}

 Final reserved keyword


* this variables value can not be modify, this is just read-only value., using final we can set
its constant.

* Note:- final variable name should be all capital


----------------------------------------------------------------------------------------------------------------
public class Test {

public static void main(String[] args) {

final float PI = 3.14f;


//PI = 3.20f;
System.out.println(PI);
}
}
=================================================================
=

 what is Data type?


* data type decide how much space should be given to variable inside memory.

* types of data types


* int 4 bytes(64 bits), 2 bytes (32 bits)
* float 4 bytes
* double 8 bytes
* String depned on character
* boolean 1 byte
* char 1 byte
* void nothing..emptiness *
----------------------------------------------------------------------------------------------------------------
public class Test {

public static void main(String[] args) {

int age = 100;


char name_initial = 'A';
String name = "Amar";
float salary = 100.20f;
double expense = 2131.50;
boolean present = true;
System.out.println(age);
System.out.println(name_initial);
System.out.println(name);
System.out.println(salary);
System.out.println(expense);
System.out.println(present);

//string concatination (append)


System.out.println(age + " " +name_initial + " " + name + " " +salary + " " + expense +
" " + present);
}
}
=================================================================
=

 what is an operator?

* operators are special type of symbols on keyboard, which can be use for calculation and
manipulation.

* types of an operator

1. Arithmatic opertor
2. Assigment operator
3. Increment or decrement operator
4. Relational or comparsional operator
5. Logical operator
6. Ternary operator
7. Bitwise operator
----------------------------------------------------------------------------------------------------------------
public class Test {

public static void main(String[] args) {

1. Assigment opertor - assing right side value to left side variale


//= += -= /= *= %=

int x = 10, y = 30,res = 0;


res = x + y;
System.out.println("Addition is " + res);
x = x + 5;
System.out.println(x);

x+=10;//x = x + 5;
System.out.println(x);

}
}
----------------------------------------------------------------------------------------------------------------

2.Increment or Decrement opertor - increae or decrease by 1


++, --

types of increment or decrement

1. post-increment or post-decrement
use ++ after variable name
like
x++
x--

2. pre-incremnet or pre-decrement
use ++ before variable name
like
++x
--x
-------------------------------------------------------
public class Test {

public static void main(String[] args) {


int x = 10;

x++;
System.out.println(x);//11

x--;
System.out.println(x);//10

++x;
System.out.println(x);//11

--x;
System.out.println(x);//10

}
}
Date:-21/09/2022
3.Increment or decrement operator

post-increment or post-decrement
x++;
x--;

Note - when we use ++ or -- inside printf, then old value will show and updated value will be
new one.

pre-incrmenet or pre-decrement
++x;
--x;

*/
public class Main{
public static void main(String[] args) {
int x = 4;
x++;
--x;
System.out.println(x++);//5
x = x + 3;
x++;
System.out.println(x--);
System.out.println(x);
System.out.println(x--);
System.out.println(x++);
System.out.println(++x);

}
}
----------------------------------------------------------------------------------------------------------------
4. Relational or comparsional operator
>
<
>=
<=
==
!=

if we want to compare two values or two variabls then us relational operator, it will return
true or false.

*/
public class Main{
public static void main(String[] args) {
int x = 4,y = 20,a = 10,b=10;
boolean res;//true or false

res = x > y;//condition


System.out.println(res);
System.out.println(x > y);

res = (x < y);//condition


System.out.println(res);

res = (x == y);//condition
System.out.println(res);

res = (x != y);//condition
System.out.println(res);

res = a > b;//condition


System.out.println(res);

res = a >= b;//condition


System.out.println(res);

res = a <= b;//condition


System.out.println(res);
}
}
----------------------------------------------------------------------------------------------------------------
5. logical operator

if we want to compre two or more than two condition it will return true of false.

&& - and
|| - or
! - not

&& - truth table


1st condition 2nd condition result
true true true
true false false
false true false
false false false

public class Main{


public static void main(String[] args) {

int x = 4,y = 20,a = 10,b=10;


boolean res;//true or false
res = (x < y) && (a == b);
System.out.println(res);

res = (x < y) && (a != b);


System.out.println(res);

res = (x > y) && (a == b);


System.out.println(res);

res = (x > y) && (a != b);


System.out.println(res);

}
}
----------------------------------------------------------------------------------------------------------------

|| or truth table

1st condition 2nd condition result


true true true
true false true
false true true
false false false

public class Main{


public static void main(String[] args) {

int x = 4,y = 20,a = 10,b=10;


boolean res;//true or false

res = (x < y) || (a == b);


System.out.println(res);

res = (x < y) || (a != b);


System.out.println(res);

res = (x > y) || (a == b);


System.out.println(res);

res = (x > y) || (a != b);


System.out.println(res);

}
}
----------------------------------------------------------------------------------------------------------------
! not truth table
1st condition result
true false
false true
*/
public class Main{
public static void main(String[] args) {

int x = 4,y = 20,a = 10,b=10;


boolean res;//true or false

res = !(x < y);


System.out.println(res);

res = (x > y) || !(a == b);


System.out.println(res);

res = !(x < y) || !(a != b);


System.out.println(res);

res = !(x > y) && !(a == b);


System.out.println(res);

res = !( (x > y) || (a != b) );
System.out.println(res);

}
}
=================================================================
=

Date:-22/09/2022
3.Ternary operator
is use to show output like some messages, like number, float ,double, values, even true and
false, but with the help of true and false, basis of true and false, if we want to print some data
then we use this operator

symbols
?:
syntax
(condition) ? true : false;

Note :
1. this is also called short hand of if conditional
2. we can not use multiple statements in this operator

*/
public class Main{

public static void main(String[] args) {


int age = 100;
String msg = (age>=18) ? "you can vote" : "you can not vote";
System.out.println(msg);
}
}
=================================================================
=

 How To Get Input From User In Java?

 we have 3 options to get data from user.


1. using Scanner Class
2. using BufferedInputReader
3. using command line arguments

//1st step - import Scanner class - inbuilt class

import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter your age : ");
int age = s.nextInt();

//4th step - print data


System.out.println("your age is : "+age);

}
}
----------------------------------------------------------------------------------------------------------------
//1st step - import Scanner class - inbuilt class
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter value x : ");
int x = s.nextInt();
System.out.print("Enter value y: ");
int y = s.nextInt();

//4th step - print data


System.out.println("addition is : "+ (x + y));
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter value of x : ");
float x = s.nextFloat();

System.out.print("Enter value of y: ");


float y = s.nextFloat();

//4th step - print data


System.out.println("addition is : "+ (x + y));

}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter value of x : ");
double x = s.nextDouble();

System.out.print("Enter value of y: ");


double y = s.nextDouble();

//4th step - print data


System.out.println("addition is : "+ (x + y));
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("r u present todays class ? ");
boolean x = s.nextBoolean();

//4th step - print data


System.out.println("presnenty status is : "+ x);
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter your name ");
String name = s.nextLine();

//4th step - print data


System.out.println("your name is : "+ name);
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter 1st no ");
//int no1 = s.nextLine(); //error: incompatible types: String cannot be converted to int
int no1 = Integer.parseInt(s.nextLine());

System.out.print("Enter 2nd no ");


int no2 = Integer.parseInt(s.nextLine());

//4th step - print data


System.out.println("addition is : "+ (no1 + no2));
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter 1st no ");
//int no1 = s.nextLine(); //error: incompatible types: String cannot be converted to int
String no1 = s.nextLine();

System.out.print("Enter 2nd no ");


String no2 = s.nextLine();

//4th step - print data


System.out.println("addition is : "+ (Integer.parseInt(no1) +
Integer.parseInt(no2)));
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter 1st no ");
//int no1 = s.nextLine(); //error: incompatible types: String cannot be converted to int
float no1 = Float.parseFloat(s.nextLine());

System.out.print("Enter 2nd no ");


float no2 = Float.parseFloat(s.nextLine());

//4th step - print data


System.out.println("addition is : "+ (no1 + no2));
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package

public class Main{

public static void main(String[] args) {

//2nd step - object or ref. creation of Scanner class


Scanner s = new Scanner(System.in);

//3rd step - get data


System.out.print("Enter char :");
String ch = s.nextLine();

//4th step - print data


System.out.println(ch.charAt(0));
System.out.println(ch.charAt(1));
System.out.println(ch.charAt(2));
}
}
=================================================================
=
 BufferedReader class
if we want to take data from user then we use Scanner class, and BufferedReader class
in Scanner class we can access data from user of primitve data type.
(int,char,float,double,boolea,String)

BufferedReader takes data byte by byte.., it is also called streaming..

Steps 1st -
import BufferedReader from java.io.* package
Note - * means all -> all class file from io folder

2nd step -
object create of BufferedReader

3rd step
take value or data using readLine()

*/

//step 1 - import
import java.io.*;

class Main{
public static void main (String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {
String name = br.readLine();
System.out.println("your name is "+name);
} catch(Exception e) {
System.out.println(e);
}
}
}
----------------------------------------------------------------------------------------------------------------
import java.io.*;

class Main{

public static void main (String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.print("Enter 1st number : ");
int n1 = Integer.parseInt(br.readLine());

System.out.print("Enter 2nd number : ");


int n2 = Integer.parseInt(br.readLine());

int sum = n1 + n2;


System.out.println("sum is : "+sum);

} catch(Exception e) {
System.out.println(e);
}
}
}
----------------------------------------------------------------------------------------------------------------
import java.io.*;

class Main{

public static void main (String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.print("Are you Present : ");
String s = br.readLine();

if(s.equals("yes")){
System.out.println("Present");
}else{
System.out.println("Abstent");
}

} catch(Exception e) {
System.out.println(e);
}
}
}

=================================================================
=

 Command line args


 we dont need to import any class.
 we can not give any prompt or hint to user like enter your age.
 data must be given at runtime in cmd with java filename.
 data as input will be in string of array fromat.
 this line is responsible to take data
String[] args

public class Main{

public static void main(String[] args) {

//to take data from user we need to use args - as arguemnt of String
System.out.println(args.length);
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
//note if data count is not matched with index number
//then exception will occure
}
}
----------------------------------------------------------------------------------------------------------------
public class Main{

public static void main(String[] args) {

//to take data from user we need to use args - as arguemnt of String
if(args.length == 3){
System.out.println(args[0] + args[1] + args[2]);
}else{
System.out.println("pls enter only 3 values");
}
}
}
What Is Function/Methods?
function is a group of or block of related code under one unit name.

function is a like action ,


i.e. tiger (animal) has action like
hunt, run,walk, eat (methods)
student
study, buck, exam,

how to identify a function?


1. in word if there is (){}
2. in any word if there is ();

why we use function?


 to avoid code repeation
 to avoid time wating task
 low space requried for methods

synatx of function
access_specifier return_data_type function_name(){
logic....
}

Note:
if we write a function, then to use it. we need to call it inside main function or in any
function.

steps to write a function/method


step 1 = function declration or function writing inside class anywhere we can create function

Note = do not write method inside main function main function is only use for calling
fuction.

step 2 = after writing a function we need to use it.& to use it we need to first call it. &
to call function we need to create object of a class.

if we do not want to create object of a class then our method should be static.

Note - what is static, static is use to access data of a class without object creation.
 Types Of An Methods
there are two majour types of method

1. Inbuilt Methods

This method are created by java itself for developer we do not create this type of method.

2. User Defiend Methods

this method are create by developer this has 4 types

1. Simple Method
2. Parameter Method
3. Return Type Method
4. Return Type With Parameter Method

1. simple method

how to identify simple method


1. in method name if there is void keyword
2. in method name parameter is empty

Note :

This fuctions are 100% dumb method because all bellow 3 things can be done by this method
1. variable creation
2. apply logic
3. print or show output
*/
public class Main{

//void means nothing, or emptiness


//step 1 - create a function
public static void add(){//parameter ()

//variable create
int x = 4, y = 4,sum=0;

//logic apply
sum = x + y;

//print or show output


System.out.println("sum is " + sum);

}
public static void sub(){
//variable create
int x = 4, y = 4,s=0;

//logic apply
s = x - y;

//print or show output


System.out.println("sb is " + s);
}

//do not write function insie main function


public static void main(String[] args) {

//2nd step - use function or call function


add();
sub();
add();
}
}
----------------------------------------------------------------------------------------------------------------

public class Main{

//void means nothing, or emptiness


//step 1 - create a function
public void add(){//parameter ()

//variable create
int x = 4, y = 4,sum=0;

//logic apply
sum = x + y;

//print or show output


System.out.println("sum is " + sum);

//do not write function insie main function


public static void main(String[] args) {

//2nd step - use function or call function


Main m = new Main();
m.add();

}
}
----------------------------------------------------------------------------------------------------------------
public class Main{

//void means nothing, or emptiness


//step 1 - create a function
public static void add(){//parameter ()

//variable create
int x = 4, y = 4,sum=0;

//logic apply
sum = x + y;

//print or show output


System.out.println("sum is " + sum);

//do not write function insie main function


public static void main(String[] args) {

//2nd step - use function or call function


Main.add();

}
}
----------------------------------------------------------------------------------------------------------------
class Cal{

public void add(){


int x = 4, y = 4,sum=0;
sum = x + y;
System.out.println("sum is " + sum);
}

public class Main{

public static void main(String[] args) {

Cal c = new Cal();


c.add();

}
}
----------------------------------------------------------------------------------------------------------------
Using Static Keyword

class Cal{
public static void add(){
int x = 4, y = 4,sum=0;
sum = x + y;
System.out.println("sum is " + sum);
}
}

public class Main{

public static void main(String[] args) {

Cal.add();

}
}

=================================================================
=

2. Parameterized Method

how to identify this method


1. in method name if there is void keyword
2. do not empty parameter

Note :

this fuctions are 50% smart method because all bellow 3 things can be done by this method

1. apply logic
2. print or show output
*/
public class Main
{
public static void sum(int x,int y){//paramter
//logic apply
int s = x + y;
//print data
System.out.println(s);
}
public static void main(String[] args) {
int x = 4, y = 3;
sum(3,4);//arguemnts
sum(30,45);//arguemnts
sum(x,y);
sum(y,x);
}
}
----------------------------------------------------------------------------------------------------------------
class A{
public void sum(int x,int y){//paramter
//logic apply
int s = x + y;
//print data
System.out.println(s);
}
}
public class Main
{
public static void main(String[] args) {
int x = 4, y = 3;
A a = new A();
a.sum(3,4);//arguemnts
a.sum(30,45);//arguemnts
a.sum(x,y);
a.sum(y,x);
}
}
=================================================================
=

3.Return Type Method

how to identify this method

1. in method name if there is int,float, double,boolean, char, or string


instead of void keyword

2. empty parameter

Note :

this fuctions are 50% smart method because all bellow 3 things can be done by this method
1. variable create
2. apply logic

this fuctions will throw or return data.it wont print data.


this fuctions is not take responsibilty of data pritng.
this is responsibilty of function calling.

class A{
public int sum(){
int x = 4, y = 3,s = 0;
s = x + y;
return s;
}
}
public class Main
{
public static void main(String[] args) {
A a = new A();
//catch value
System.out.println(a.sum());
int x = a.sum();
System.out.println(x);
}
}
----------------------------------------------------------------------------------------------------------------
class A{
public int sum(){
int x = 4, y = 3;
return x+y;
}
public float sub(){
float x = 4.50f, y = 3.6f;
return x-y;
}
}
public class Main
{
public static void main(String[] args) {
A a = new A();
//catch value
System.out.println(a.sum());

int x = a.sum();
System.out.println(x);
System.out.println(a.sub());
}
}
----------------------------------------------------------------------------------------------------------------
class A{
public int sum(int x,int y){
return x+y;
}
public float sub(float x,float y){
return x-y;
}
}
public class Main
{
public static void main(String[] args) {

A a = new A();
//catch value
System.out.println(a.sum(3,2));

int x = a.sum(5,4);
System.out.println(x);
System.out.println(a.sub(34,6));
}
}
 Function Overloading

class A{
public int sum(int x,int y){
return x+y;
}
public int sum(int x,int y,int z){
return x+y+z;
}
public float sum(float x,float y){
return x+y;
}
}
public class Main
{

public static void main(String[] args) {

A a = new A();
System.out.println(a.sum(3,2));
System.out.println(a.sum(3.50f,2.5f));
System.out.println(a.sum(3,20,10));

}
}
----------------------------------------------------------------------------------------------------------------

 Java Simple for Loop


 A simple for loop is the same as C/C++. We can initialize the variable, check
condition and increment/decrement value. It consists of four parts:

1. Initialization: It is the initial condition which is executed once when the loop starts.
Here, we can initialize the variable, or we can use an already initialized variable. It
is an optional condition.
2. Condition: It is the second condition which is executed each time to test the
condition of the loop. It continues execution until the condition is false. It must
return boolean value either true or false. It is an optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.
4. Statement: The statement of the loop is executed each time until the second
condition is false.

Syntax:

for(initialization; condition; increment/decrement){

//statement or code to be executed

----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345
int sum = 0;
for(int i=1;i<=5;i++){
sum = sum + i;
}
System.out.println(sum);
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 - show every even and odd.
//logic and output will be inside for loop
for(int i=1;i<=5;i++){
if(i%2==0){
System.out.println(i + " is even");
}else{
System.out.println(i + " is odd");
}
}

}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 - count of even and odd
//logic will be inside for loop
//output outside for loop
int e=0,o=0;
for(int i=1;i<=5;i++){
if(i%2==0){
e++;
}else{
o++;
}
}
System.out.println("count of even "+e);
System.out.println("coutn of odd "+o);

}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 - count of even and odd
//logic will be inside for loop
//output outside for loop
int esum=0,osum=0;
for(int i=1;i<=5;i++){
if(i%2==0){
esum = esum + i;
}else{
osum = osum + i;
}
}
System.out.println("sum of even is "+esum);
System.out.println("sum of odd is "+osum);

}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 - show every numbe as pos or neg
for(int i=1;i<=5;i++){
if(i>0){
System.out.println(i + " is pos no");
}else{
System.out.println(i + " is neg no");
}
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 -
int p = 0, n = 0;
for(int i=1;i<=5;i++){
if(i>0){
p++;
}else{
n++;
}
}
System.out.println("count of pos : " + p);
System.out.println("count of neg : " + n);
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
//12345 -
int max = 0, min=0;
for(int i=1;i<=5;i++){
if(i>max){
max = i;
}if(i<min){
min = i;
}
}
System.out.println("max " + max);
System.out.println("min : " + min);

}
}
----------------------------------------------------------------------------------------------------------------
/*

* * * R 1 (c1,c2,c3)
* * * R 2 (c1,c2,c3)
* * * R 3 (c1,c2,c3)

Row
start = 1 int row = 1;
condition = 1<=3 row<=3
step = 1++ row++
col
start = 1 int col = 1;
condition = 1<=3 col<=3;
step = 1++ col++

for(row){
for(col){
//printing...
}
*/
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 1;col<=3;col++){
System.out.print("* ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 1;col<=3;col++){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 3;col>=1;col--){
System.out.print(col);
}
System.out.println();
}
}
}

-
-
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 2;col<=6;col=col+2){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 1;col<=7;col=col+2){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(char col = 'A';col<='D';col++){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=3;row++){
for(int col = 1;col<=3;col++){
System.out.print(row);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=3;row>=1;row--){
for(int col = 1;col<=3;col++){
System.out.print(row);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=4;row++){
for(int col = 1;col<=row;col++){
System.out.print("* ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=4;row++){
for(int col = 1;col<=row;col++){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------

public class Main


{
public static void main(String[] args) {

for(int row=1;row<=4;row++){
for(int col = 3;col>=row;col--){
System.out.print(col);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=4;row++){
for(int col = 3;col>=row;col--){
System.out.print("* ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {

for(int row=1;row<=4;row++){

for(int sp = 3;sp>=row;sp--){
System.out.print(" ");
}

for(int col = 1;col<=row;col++){


System.out.print("*");
}

System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------

 java While Loop


The Java while loop is used to iterate a part of the program repeatedly until
the specified Boolean condition is true.

import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main(String[] args) {

int x = 1;//true

while (x==1){
System.out.print("Enter value : ");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if(i==0){
x = 0;
}else{
Random rd = new Random(10);
System.out.println(rd.nextInt(i)+1);
}
}

}
}
----------------------------------------------------------------------------------------------------------------

import java.util.Scanner;
import java.util.Random;

public class Main


{
public static void main(String[] args) {

int x = 1;//true
do{
System.out.print("Enter value : ");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if(i==0){
x = 0;
}else{
Random rd = new Random(10);
System.out.println(rd.nextInt(i)+1);
}
}
while (x==1);
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {

int choice;

do{
System.out.println("-----Welcome to My Bank------");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance Enquery");
System.out.println("4. Exit");

Scanner s = new Scanner(System.in);

System.out.print("Enter your choice : ");


choice = s.nextInt();

System.out.print("\033[H\033[2J");
System.out.flush();

if(choice==1){
System.out.println("Withdraw");
}else if(choice == 2){
System.out.println("Deposit");
}else if(choice == 3){
System.out.println("Balance Enquery");
}else if(choice == 4){
System.out.println("Bye Bye");
}else{
System.out.println("pls enter choice between 1,2,3,4 only");
}
}while (choice!=4);
}
}
----------------------------------------------------------------------------------------------------------------
 break –

1. break is reserved keyword


2. break is use to terminate the loop.
3. break is only use with loop and switch
*/
public class Main
{
public static void main(String[] args) {
for(int i=1;i<=5;i++){
if(i==4){
break;
}
System.out.println(i);
}
}
}
----------------------------------------------------------------------------------------------------------------

continue –

1. continue is reserved keyword


2. continue is use to skip the loop on certain condition
*/

public class Main


{
public static void main(String[] args) {
for(int i=1;i<=5;i++){
if(i==4){
continue;
}
System.out.println(i);
}
}
}
----------------------------------------------------------------------------------------------------------------
continue -
1. continue is reserved keyword
2. continue is use to skip the loop on certain condition
*/

public class Main


{
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(i);
if(i==4){
continue;
}
//there is nothing to skip
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(i);
if(i==4){
break;
}

}
}
}
----------------------------------------------------------------------------------------------------------------
 Switch Case Statement

 every switch program will convert to if conditional statement, but all if conditional
program wont convert to switch we matches the value with case.

Note
switch case only support int, char, enum .

 there are 4 reserved keyword


1. switch
2. break
3. default
4. case

public class Main


{
public static void main(String[] args) {

int day = 20;//int

switch(day){
case 1://int
System.out.println("SUN");
break;

case 2://int
System.out.println("MON");
break;
case 3://int
System.out.println("TUE");
break;

case 4://int
System.out.println("WED");
break;

case 5://int
System.out.println("THU");
break;

case 6://int
System.out.println("FRI");
break;

case 7://int
System.out.println("SAT");
break;

default:
System.out.println("pls enter days between 1 to 7 only");
break;

}
}
----------------------------------------------------------------------------------------------------------------

public class Main


{
public static void main(String[] args) {

char ch = 'a';

switch(ch){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is vowel");
break;

default:
System.out.println(ch + " is non vowel");
break;

}
}
----------------------------------------------------------------------------------------------------------------

public class Main


{
public static void main(String[] args) {

char opr = '+';


int x = 5,y = 3;
switch(opr){
case '+':
System.out.println("addition is " + (x + y));
break;

case '-':
System.out.println("substraction is " + (x - y));
break;

case '*':
System.out.println("mult is " + (x + y));
break;

case '/':
System.out.println("div is " + (x + y));
break;

case '%':
System.out.println("reminder is " + (x + y));
break;

default:
System.out.println("pls enter opr between +, -, / , *");
break;

}
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class Main


{
public static void main(String[] args) {

int choice,amt,current_balance = 1000;

do{

System.out.println("------Welcome to Bank----");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance Enquery");
System.out.println("4. Logout");

Scanner s = new Scanner(System.in);


System.out.print("Enter your choice : ");
choice = s.nextInt();

//clear screen
System.out.print("\033[H\033[2J");
System.out.flush();

switch(choice){
case 1:
System.out.println("------Welcome to Withdraw screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
if(amt>current_balance){
System.out.println("you dont have sufficent Balance");
System.out.println();
}else{
current_balance = current_balance - amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Withdraw successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}
break;

case 2:
System.out.println("------Welcome to Deposit screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
current_balance = current_balance + amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Deposit successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
break;

case 3:
System.out.println("------Welcome to Balance Enquery screen-----");
System.out.println("your current Balance is " + current_balance);
System.out.println();
break;

case 4:
System.out.println("Bye Bye");
break;

default:
System.out.println("pls enter choice between 1,2,3,4 only");
break;

}
}while (choice!=4);

}
}
----------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class Main


{
static int choice,amt,current_balance = 1000;
static Scanner s;

public static void starter(){

s = new Scanner(System.in);

System.out.println("------Welcome to Bank----");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance Enquery");
System.out.println("4. Logout");
System.out.print("Enter your choice : ");
choice = s.nextInt();

//clear screen
System.out.print("\033[H\033[2J");
System.out.flush();

public static void withdraw(){


System.out.println("------Welcome to Withdraw screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
if(amt>current_balance){
System.out.println("you dont have sufficent Balance");
System.out.println();
}else{
current_balance = current_balance - amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Withdraw successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}
}

public static void deposit(){


System.out.println("------Welcome to Deposit screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
current_balance = current_balance + amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Deposit successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}

public static void balance_enquery(){


System.out.println("------Welcome to Balance Enquery screen-----");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}

public static void logout(){


System.out.println("Bye Bye");
}
public static void message(){
System.out.println("pls enter choice between 1,2,3,4 only");
}

public static void main(String[] args) {


do{
starter();

switch(choice){
case 1:
withdraw();
break;

case 2:
deposit();
break;

case 3:
balance_enquery();
break;

case 4:
logout();
break;

default:
message();
break;
}
}while (choice!=4);

}
}
----------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

class Bank{

static int choice,amt,current_balance = 1000;


static Scanner s;

public static void starter(){

s = new Scanner(System.in);

System.out.println("------Welcome to Bank----");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance Enquery");
System.out.println("4. Logout");

System.out.print("Enter your choice : ");


choice = s.nextInt();

//clear screen
System.out.print("\033[H\033[2J");
System.out.flush();

public static void withdraw(){


System.out.println("------Welcome to Withdraw screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
if(amt>current_balance){
System.out.println("you dont have sufficent Balance");
System.out.println();
}else{
current_balance = current_balance - amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Withdraw successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}
}

public static void deposit(){


System.out.println("------Welcome to Deposit screen-----");
System.out.print("Enter amt : ");
amt = s.nextInt();
current_balance = current_balance + amt;
System.out.println("Sucess!!!");
System.out.println(amt + " is Deposit successfully");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}

public static void balance_enquery(){


System.out.println("------Welcome to Balance Enquery screen-----");
System.out.println("your current Balance is " + current_balance);
System.out.println();
}
public static void logout(){
System.out.println("Bye Bye");
}

public static void message(){


System.out.println("pls enter choice between 1,2,3,4 only");
}

public class Main{

public static void main(String[] args) {

do{
Bank.starter();

switch(Bank.choice){
case 1:
Bank.withdraw();
break;

case 2:
Bank.deposit();
break;

case 3:
Bank.balance_enquery();
break;

case 4:
Bank.logout();
break;

default:
Bank.message();
break;
}
}while (Bank.choice!=4);

}
}
=================================================================
=

Array –
 if we want to store mutliple values inside single variable then its not possible, to solve
this issue we an array concept,

what is an array ?
array is use to store multiple values of same data type.

how to identfiy array?


in any word if there is []

array has two types


1. 1d array
one row and multiple cols

2. 2d array
multiple rows, multiple cols

* to access data of array we need index numher, this index start with 0

----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {

int x[] = {3,43,54};


//array using index
System.out.println(x[0]);
System.out.println(x[1]);
System.out.println(x[2]);
//System.out.println(x[3]);

System.out.println(x[0] + " " + x[1] + " " + x[2]);

for(int i=0;i<3;i++){
System.out.println(x[i]);
}

}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {

int x[] = {3,43,54};

x[0] = 100;
x[2] = 99;

for(int i=0;i<3;i++){
System.out.println(x[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------

public class Main{


public static void main(String[] args) {

int x[] = {3,43,54},sum = 0;

for(int i=0;i<3;i++){
sum = sum + x[i];
}
System.out.println(sum);

}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {

int x[] = {3,43,-54},sum = 0;

for(int i=0;i<3;i++){
if(x[i]>0){
System.out.println(x[i] + " is pos number");
}else{
System.out.println(x[i] + " is neg number");
}
}

}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {

int x[] = {3,43,-54},max = 0,min=0;

for(int i=0;i<3;i++){
if(x[i]>max){
max = x[i];
}
if(x[i]<max){
min = x[i];
}
}
System.out.println("max is " + max);
System.out.println("min is " + min);

}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class Main{


public static void main(String[] args) {

int x[] = new int[3];

Scanner s = new Scanner(System.in);

System.out.print("Enter value for 0 index : ");


x[0] = s.nextInt();

System.out.print("Enter value for 1 index : ");


x[1] = s.nextInt();

System.out.print("Enter value for 2 index : ");


x[2] = s.nextInt();

for(int i=0;i<3;i++){
System.out.println(x[i]);
}

}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class Main{


public static void main(String[] args) {

int x[] = new int[3];

Scanner s = new Scanner(System.in);

//take data from user


for(int i=0;i<3;i++){
System.out.print("Enter value for " + i + " index : ");
x[i] = s.nextInt();
}

//to print data to user


for(int i=0;i<3;i++){
System.out.println(x[i]);
}

}
}
----------------------------------------------------------------------------------------------------------------
 2d array

234
123
545

import java.util.Scanner;

public class Main{


public static void main(String[] args) {

int x[][] = {
{2,3,4}, //row 0 (col 0, col 1, col 2)
{1,8,7}, //row 1 (col 0, col 1, col 2)
{0,5,3} //row 2 (col 0, col 1, col 2)
};

System.out.println(x[0][0] + " " + x[0][1] + " " + x[0][2]);


System.out.println(x[1][0] + " " + x[0][1] + " " + x[0][2]);
System.out.println(x[2][0] + " " + x[0][1] + " " + x[0][2]);

for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
System.out.print(x[row][col]);
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
234
123
545
*/
import java.util.Scanner;

public class Main{


public static void main(String[] args) {

int x[][] = new int[2][3];


Scanner s = new Scanner(System.in);

System.out.println("Enter value of 0 row & 0 col : ");


x[0][0] = s.nextInt();

System.out.println("Enter value of 0 row & 1 col : ");


x[0][1] = s.nextInt();

System.out.println("Enter value of 0 row & 2 col : ");


x[0][2] = s.nextInt();

System.out.println();

System.out.println("Enter value of 1 row & 0 col : ");


x[1][0] = s.nextInt();

System.out.println("Enter value of 1 row & 1 col : ");


x[1][1] = s.nextInt();

System.out.…
----------------------------------------------------------------------------------------------------------------
2d array

234
123
545

*/
import java.util.Scanner;

public class Main{


public static void main(String[] args) {

int x[][] = new int[2][3];


Scanner s = new Scanner(System.in);

for(int row=0;row<2;row++){
for(int col=0;col<3;col++){
System.out.print("Enter value of 0 row & 0 col : ");
x[row][col] = s.nextInt();
}
System.out.println();
}

for(int row=0;row<2;row++){
for(int col=0;col<3;col++){
System.out.print(x[row][col]);
}
System.out.println();
}

}
}
----------------------------------------------------------------------------------------------------------------
2d array

234
123
545

*/
import java.util.Scanner;

public class Main{


public static void show(){
int x[][] = new int[2][3];
Scanner s = new Scanner(System.in);

for(int row=0;row<2;row++){
for(int col=0;col<3;col++){
System.out.print("Enter value of 0 row & 0 col : ");
x[row][col] = s.nextInt();
}
System.out.println();
}

for(int row=0;row<2;row++){
for(int col=0;col<3;col++){
System.out.print(x[row][col]);
}
System.out.println();
}
}
public static void main(String[] args) {

Main.show();

}
}
----------------------------------------------------------------------------------------------------------------
2d array

234
123
545

*/
import java.util.Scanner;

public class Main{

public static void show(int r, int c){

int x[][] = new int[r][c];


Scanner s = new Scanner(System.in);

for(int row=0;row<r;row++){
for(int col=0;col<c;col++){
System.out.print("Enter value of 0 row & 0 col : ");
x[row][col] = s.nextInt();
}
System.out.println();
}

for(int row=0;row<r;row++){
for(int col=0;col<c;col++){
System.out.print(x[row][col]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.println("Enter row no : ");


int r = s.nextInt();
System.out.println("Enter col no : ");
int c = s.nextInt();

Main.show(r,c);

}
}
----------------------------------------------------------------------------------------------------------------
 Oops - object oriented programming System
 class - class is a group of variables and methods under unique name.

 class is a user defined data type.

 data inside class is visible to devloper

 class is totally depend on object or reference

 class is blueprint of object.

*/

class Student{

//global variables, instance variables, class level variables


String name = "ram";
int age = 10;
float fees = 32132.50f;

//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}

public class Main{

public static void main(String[] args) {


//object or reference - s is object
Student s = new Student();
s.show();
}
}
----------------------------------------------------------------------------------------------------------------

what is an object?
 object is anything which has properties(variables) and behaviours(methods).
 object is always work with class, without class we can not create an object
 all data inside object is hidden

class Student{

//global variables, instance variables, class level variables


String name = "ram";
int age = 10;
float fees = 32132.50f;

//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}

public class Main{

public static void main(String[] args) {


//object or reference - s is object
Student s = new Student();

s.show();//address of s.show in memory is diffrent form other object

Student s1 = new Student();


s1.show();

Student s2 = new Student();


s2.show();

System.out.println(s.name);

}
}
---------------------------------------------------------------------------------------------------------------

class Student{

//global variables, instance variables, class level variables


String name = "ram";
int age = 10;
float fees = 32132.50f;

//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}

public class Main{

public static void main(String[] args) {


//object or reference - s is object
Student s = new Student();

s.show();//address of s.show in memory is diffrent form other object

Student s1 = new Student();


s1.show();

Student s2 = new Student();


s2.show();

s.name = "sham";

System.out.println(s.name);

}
}
----------------------------------------------------------------------------------------------------------------
 how to set value to instance variable - we have 3 options
1. using paramter method
2. using getter setter method
3. using constructor
4. scope initializer
*/

class Student{

//global variables, instance variables, class level variables


private String name;
private int age;
private float fees;

//methods
public void setData(String n, int a, float f){//local variable
name = n;//local to global
age = a;
fees = f;
}
public void showData(){
System.out.println(name + " " + age + " " + fees);
}

public class Main{

public static void main(String[] args) {


//object or reference - s is object
Student s = new Student();
s.setData("ram",33,3.34f);
s.showData();

}
}
----------------------------------------------------------------------------------------------------------------
 this keyword
this is reserved keyword , it is use to set variable as global this means current class variables

class Student{

//global variables, instance variables, class level variables


private String name;
private int age;
private float fees;

//methods
public void setData(String name, int age, float fees){//local variable
this.name = name;//local to global
this.age = age;
this.fees = fees;
}
public void showData(){
System.out.println(name + " " + age + " " + fees);
}

public class Main{

public static void main(String[] args) {


//object or reference - s is object
Student s = new Student();
s.setData("ram",33,3.34f);
s.showData();

}
}
----------------------------------------------------------------------------------------------------------------
 what is constructor?
1. constructor is like a function, but it wont return a value,it do not need to call,
2. it will call automatically when object is creaed.
3. constructor is use to initialized instance variable, at the time of object creation

constructor has two types


1. default constructor
2. parameter constructor

*/

class Student{

Student(){
System.out.println("from default constructor");
}
}

class Main{
public static void main(String[] args) {

Student s = new Student();


}
}
----------------------------------------------------------------------------------------------------------------

You might also like