Core Java Class Notes
Core Java Class Notes
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 {
//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);
}
}
=================================================================
=
int mycurrentaddress;
int myCurrentAddress;//camelcase
int my_current_address
//variable re-initialization
x = 30;
//variable declare
int y;
System.out.println(x);
}
}
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 {
x+=10;//x = x + 5;
System.out.println(x);
}
}
----------------------------------------------------------------------------------------------------------------
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 {
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);
res = (x != y);//condition
System.out.println(res);
if we want to compre two or more than two condition it will return true of false.
&& - and
|| - or
! - not
}
}
----------------------------------------------------------------------------------------------------------------
|| or truth table
}
}
----------------------------------------------------------------------------------------------------------------
! not truth table
1st condition result
true false
false true
*/
public class Main{
public static void main(String[] args) {
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{
import java.util.Scanner;//package
}
}
----------------------------------------------------------------------------------------------------------------
//1st step - import Scanner class - inbuilt class
import java.util.Scanner;//package
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;//package
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) {
try {
String name = br.readLine();
System.out.println("your name is "+name);
} catch(Exception e) {
System.out.println(e);
}
}
}
----------------------------------------------------------------------------------------------------------------
import java.io.*;
class Main{
try {
System.out.print("Enter 1st number : ");
int n1 = Integer.parseInt(br.readLine());
} catch(Exception e) {
System.out.println(e);
}
}
}
----------------------------------------------------------------------------------------------------------------
import java.io.*;
class Main{
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);
}
}
}
=================================================================
=
//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{
//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.
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.
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.
1. Simple Method
2. Parameter Method
3. Return Type Method
4. Return Type With Parameter Method
1. simple method
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{
//variable create
int x = 4, y = 4,sum=0;
//logic apply
sum = x + y;
}
public static void sub(){
//variable create
int x = 4, y = 4,s=0;
//logic apply
s = x - y;
//variable create
int x = 4, y = 4,sum=0;
//logic apply
sum = x + y;
}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
//variable create
int x = 4, y = 4,sum=0;
//logic apply
sum = x + y;
}
}
----------------------------------------------------------------------------------------------------------------
class Cal{
}
}
----------------------------------------------------------------------------------------------------------------
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);
}
}
Cal.add();
}
}
=================================================================
=
2. Parameterized Method
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);
}
}
=================================================================
=
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
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
{
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));
}
}
----------------------------------------------------------------------------------------------------------------
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:
----------------------------------------------------------------------------------------------------------------
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();
}
}
}
----------------------------------------------------------------------------------------------------------------
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(" ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------
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;
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");
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 –
continue –
}
}
}
----------------------------------------------------------------------------------------------------------------
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 .
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;
}
}
----------------------------------------------------------------------------------------------------------------
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;
}
}
----------------------------------------------------------------------------------------------------------------
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;
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");
//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;
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();
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{
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");
//clear screen
System.out.print("\033[H\033[2J");
System.out.flush();
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.
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) {
for(int i=0;i<3;i++){
System.out.println(x[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {
x[0] = 100;
x[2] = 99;
for(int i=0;i<3;i++){
System.out.println(x[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------
for(int i=0;i<3;i++){
sum = sum + x[i];
}
System.out.println(sum);
}
}
----------------------------------------------------------------------------------------------------------------
public class Main{
public static void main(String[] args) {
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) {
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;
for(int i=0;i<3;i++){
System.out.println(x[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
}
}
----------------------------------------------------------------------------------------------------------------
2d array
234
123
545
import java.util.Scanner;
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)
};
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;
System.out.println();
System.out.…
----------------------------------------------------------------------------------------------------------------
2d array
234
123
545
*/
import java.util.Scanner;
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;
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;
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);
Main.show(r,c);
}
}
----------------------------------------------------------------------------------------------------------------
Oops - object oriented programming System
class - class is a group of variables and methods under unique name.
*/
class Student{
//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}
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{
//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}
System.out.println(s.name);
}
}
---------------------------------------------------------------------------------------------------------------
class Student{
//methods
public void show(){
System.out.println(name + " " + age + " " + fees);
}
}
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{
//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);
}
}
}
----------------------------------------------------------------------------------------------------------------
this keyword
this is reserved keyword , it is use to set variable as global this means current class variables
class Student{
//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);
}
}
}
----------------------------------------------------------------------------------------------------------------
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
*/
class Student{
Student(){
System.out.println("from default constructor");
}
}
class Main{
public static void main(String[] args) {