Java Programming
Java Programming
In this chapter we will see what is java,why do we want to learn java,and we will see how to install java
in windows operating system java,so java is a high level programming language developed by sun
microsystems in early 90s,so like any other high level programming language like and c++ java is also a
high level programming language,but there is a difference from other programming languages that it is a
cross platform programming language,which you can develop and compile on multiple operating system
like linux,mac os and windows,and it runs the program with special virtual machine called JVM,Java
Virtual Machine,so this java virtual machine is responsible for running the java program so whenever
you install java virtual machine on your operating system,so this java virtual machine is same for every
operating system,so the program runs on the java virtual machine and not on the native linux or
windows operating system,so it jvm runs on every operating system that java supports,so you can
compile your program on mac os or linux and you can run it on windows using java virtual machine,so
before installing java you need to install java JDK or JRE on the operating systems and so java programs
are portable across platforms,each program is translated into java bytecode and each machine has a
Java Virtual Machine(JVM) which knows how to execute the bytecode.java is an object oriented
programming language
And first we need to install java on our system,to check if java is not installed or installed on your system
go to the start menu and open the command prompt
Javac
C:\Users\Programming Knowledge>javac
If this is the message shown then java is not installed on the computer,so you can install java like this
Java is platform and net beans is Ide,so you have the option to download java with netbeans ide,But we
will not choose the net beans we will choose the java JDK-Java SE Development Kit
Once java is installed you can go to the c drive and program files-java-jdk,jre
In the java folder in program files you will find jdk and jre files
And you need to set your environment variables so that while compiling the it will recognize your jdk
and jre
System variables-PathC:\ProgramData\oracle\java\javapath
There will be already a value in this Variable Value,we will check this
Program Files-java-jdk8-bin
C:\Program Files\Java\jdk8\bin
This time go to the jre folder,give this path of jre in this also the
So why do we set environment variables when you compile a program and when you set environment
variables,the compiler knows the path of these binary paths which are responsible for the file in your
program and then you can compile the program,now after setting environment variables you can open
command prompt and check this
C:\Users\Programming Knowledge>javac
Now we will see many things there so this is the response that java is installed,so whenever you give
javac program it will compile your java program,in the next chapter we will see what is eclipse IDE which
is mostly used ide for compiling java programs
In this chapter we will see how we can install eclipse IDE for our windows operating system,IDE is
integrated Development Environment which helps to code the program,so IDE is an editor,which helps
to write your program,helps to compile the program,so it is the most used IDE for java development,so
we will search the eclipse IDE for the program,we search,eclipse ide,-Eclipse Downloads
www.eclipse.org/downloads/
so we have the zip file and in this file we have the folder eclipse,we will extract the eclipse folder to the
c:\,Extract to-Extraction path and options-OS(C:)
and after extraction we can see the eclipse folder inside the C folder
C-eclipse-configurations
dropins
features
plugins
readme
eclipseproduct
artifacts
eclipse
eclipse
eclipse
notice
C:\ProgramData\Oracle\Java\Javapath\javaw.exe
This error comes when you have something wrong configured in your path
We have set the environment variables for jdk 8 and jre 8,those environment variables are not
responsible for this error,this error is due to the environment variable setting of this
C:\ProgramData\Oracle\Java\Javapath\javaw.exe
We will copy this path to the project explorer and see if this path exists
ThisPC-OS(C:)-ProgramData-Oracle-Java-Javapath
Java
Javaw
Javaws
It will show a workspace by default all the programs will be saved in this workspace
Workspace:C:\Users\ProgrammingKnowledge.workspace
Enter
File-new-javaproject
Project name:MyFirstProject
Click next
Java Settings
Source
MyFirstProject
Src
Enter
Package explorer
MyFirstProject
Src
Java class
Source Folder:MyFirstProject/src
Name:MyClass
Modifiers:public
Package explorer
MyFirstProject
Src
Default Package
MyClass.java
MyClass.java
System.out.println(“Hello World”);
}
We will right click MyFirstProject in the project explorer
Select this-MyClass.java
So this is that eclipse ide is properly configured in my system and we will compile the java program
In the next chapter we will see how to properly configure your first java project
And we will see what are there methods,classes and objects this
We have seen how we can download and install eclipse ide on our operating system,we have also seen
how we can configure eclipse to run a java program,in this chapter we will see how to create a project
and what is the structure of this eclipse IDE,and we will see how to do important settings in eclipse
IDE,we will create a new project
Project name:FirstProject
-JRE
Jdk8
So we have the execution environment Jre and also we have the jdk these paths are needed to execute
the program
click next
eclipse will create a new folder for us and also eclipse will create a sub folder for us,which will contain all
the source files and the,default output Folder will be FirstProject\bin,FirstProject is the file name and
then \ bin
we click enter
we see in the project explorer we have two projects,so in eclipse you can have as many number of
projects but you have to run the project 1 at a time
Package explorer
First Project
MyFirstProject
So to close a project you can right click on project and –close project
Package explorer
First Project
Src(right click-new-packages)
So packages are used to bind different workspaces or different folders in your class
Suppose you want to take a program for two different cases in one project then you can create a
package for both the cases for example I want to create package for FirstProject,so I want to create a
lesson 1 case then we go to the package
Java Package
Source folder:FirstProject/src
Name:lesson1
Click enter
Package explorer
First Project
Src
lesson 1
JRE System Library
So this will create a new package folder in the source folder src
Programming knowledge-
Contacts
Desktop
Documents
Workspace(click on this)-
.metadata
.recommenders
FirstProject(click on this)-
MyFirstProject
.settings
bin
src(click on this)-
.classpath
.project
lesson 1
So we go by this
So whenever you create a package there will be a folder inside your source folder
Java class
Source folder:FirstProject/src
Package:lesson 1
Check
Click enter
Package explorer
First Project
Src
lesson 1
MyClass.java
MyClass.java
Package lesson 1;
So every java program has atleast 1 class,and inside this class it has at least one method called main
Basic
CVS
Debug
Git
Java(click on this)
Click edit
-edit
So every java program has atleast 1 java class whatever class you declare and atleast 1 method called
main,so a method is a pieace of code that does a action so this method,public static void main,we will
see in detail but for now,every program has 1 main methos and whatever you write inside these curly
braces after this main method will be executed so for example,we will see what commnets are for
example
For example we want to define what is the purpose of this class or what this program is doing,so to
make a comment give forward slash /* enter eclipse will create this type of comments for us that
/*
*/
MyClass.java
Package lesson 1;
/*
*/
So this is 1 way of writing comments and the other way of writing comments is this by using // and this
will also not be executed
MyClass.java
Package lesson 1;
*/
So whenever you create a project it has a class MyClass and a method,and you can see there is a
perspective on the right side of the window called outline
Outline
Lesson1(package)
MyClass
Main(String[]:void
So your package,your class and your main function are listed here this
So when your program becomes then this outline will help you to access your class,your methods and
packages wherever they are
So when you click in the outline MyClass the cursor goes to MyClass in the program similarly for the
methods and packages,so whatever you write inside the main method will be executed,so java has
already inbuild classes to help you program
Write System. So in the pop up eclipse ide will show what are the methods available in this system class
MyClass.java
Package lesson 1;
/*
System.out.println(“Hello Youtube”);
Suppose we want to run the program then right click FirstProject in the project explorer and then run as
Java Application
Or you can directly run the program you will get this save and launch,we see the output
Output
Hello Youtube
So this system is a class so whenever you hover,move the cursor, over ant inbuilt class,it will give the
help pop up in this,for example system.
We see this
Javalang.System
The system class contains several useful class fields and methods,it cannot be instantiate
among the facilities provides by system class are standard input,standard output and error output
streams,access to externally defined properties and environment variables and means of loading files
and libraries and a utility method for quickly copying a portion of an array
similarly you can move the cursor over out and print and see the description or you can press ctrl and
then click the method you would be redirected on the other page for seeing the description
System.class
/***
* the <code> system</code> class contains several useful classes and method.
* it cannot be instantiated
*<p>among the facilities provided by the code system</cin></cout>are standard input ,standard output
*and error output,access to properties and externally defined environment variables,a means of loading
*files and libraries,and a method for quickly copying a portion of the array
*@author unsubscribed
*@since JDK1.0
***/
*Note that to use the properties set by VM,see the described in the initializeSystemClass method
*/
Static{
Register natives();
Private system(){
/***
*/
Public final static input stream in=null;
/***
* the standard output stream,this stream is already open and ready to accept output data
* typically this corresponds to display output or another description specified by the host
* environment or user
*<p>
* @see java.io.PrintStream#println()
So this is System.class
So we have seen that we can get help for the description of the methods as seen above
In this chapter we will see what are variables and how we can use variables in java,so variables are
names that we can use to declare to store a value,and in java there is a specified format to declare a
variable this,so in java you want to declare a variable whose main method is there,so we will declare a
variable like this
MyClass.java
Package lesson 1;
/*
*/
Public static void main(string[] args){
System.out.println(“Hello Youtube”);
We will select the data type,so data type is type of variable this,suppose we want to store integer in a
variable value this,we can declare a data type int which stands for integer,then you can give any name to
the variable this int x and then we will assign a value to this variable
Int x=10;
This is that x is a variable of type integer which has a value of 10,so x in this case is variable,and variables
are names that can store a value,suppose you want to print the value of x you can directly print the
variable
System.out.println(“x”);
MyClass.java
Package lesson 1;
/*
*/
Int x=10;
System.out.println(“x”);
}
Output
10
Int my_variable=10
MyClass.java
Package lesson 1;
/*
*/
Int my_variable=10;
System.out.println(“my_variable”);
Output
My variable 10
In java there are many data types that we can declare here we have declared data type of type integer
int,so we will see some of the data types
MyClass.java
Package lesson 1;
/* byte(number,1 byte)
Short(number,2 bytes)
Int(number,4 bytes)
long(number,8 bytes)
byte my_variable=10;
float my_decimal=(float)4.5;
double my_double=13.58;
char my_char=’A’;
boolean is_true=false;
System.out.println(“my_variable”);
}
We write (float) for 4.5 becuae 4.5 can be double so we specify this 4.5 is float by giving(float)
This (float) is that we are type casting double value to float,so these are the data type in java and we see
that how we can declare the variable in java,so all these variables can print the values
MyClass.java
Package lesson 1;
/* byte(number,1 byte)
Short(number,2 bytes)
Int(number,4 bytes)
long(number,8 bytes)
byte my_variable=10;
float my_decimal=(float)4.5;
double my_double=13.58;
char my_char=’A’;
boolean is_true=false;
System.out.println(“my_variable”);
System.out.println(“my_decimal”);
System.out.println(“my_double”);
System.out.println(“my_char”);
System.out.println(“is_true”);
}
Output
10
4.5
13.5
false
In this chapter we will see how we can give user input using java,suppose you want to enable user
inputs,the user should give the input,then we will do a action.so how we can this,there is a special class
in java ,suppose you want that user should give the input for the action to do this,there is a special class
in java called scanner which we used to take the input,so we define scanner so scan can be a data type
and scan can be a variable,we can define a variable for this data type or instance
To print the output on the console we write system.out and to take input we write system.in,so this
code will enable us to take the input from the user,now for the first time,we write this we see a line
below this scanner,this is because we need to add this class in our project file,so when we compile this
program it gives error,that is error in the required project,the error is that-scanner cannot be resolved
to a type,so we hover over scanner and eclipse will give this,so eclipse ide will give some suggestions,so
the first suggestion is import scanner
Import java.util.scanner
So this scanner is already defined in java,this scanner in import takes the scan from the input of the user
Now to see what input user has given you can tell the user
System.out.println(“Enter a number”);
In the next line we will define a variable,we have seen how to define a variable
Int user_input_number
Program
package lesson1;
import java.util.scanner;
System.out.println(“Enter a number”);
int user_input_number=scan.nextInt();
When you write scan. It will give suggestions,since we have int user so we will take numbers
Program
package lesson1;
import java.util.scanner;
System.out.println(“Enter a number”);
int user_input_number=scan.nextInt();
in the next line we are going to print the entered value is and then
Program
package lesson1;
import java.util.scanner;
System.out.println(“Enter a number”);
int user_input_number=scan.nextInt();
System.out.print(“user_input_number”);
We will see that we output a number,we enter a number like 1300,so the programmer is asking the user
to enter the number
Enter a number
1300
1300
Again we enter the value and it gives the number entered,this nextInt is the user enter a double value or
enter a text value,so you can do this you can copy this value from this
Program
package lesson1;
import java.util.scanner;
System.out.println(“Enter a number”);
int user_input_number=scan.nextInt();
System.out.print(“user_input_number”);
double user_input_double=scan1.nextDouble();
System.out.print(“user_input_number”);
So whatever the input you are taking we give the next double,so the user will input double values this,so
first we will ask the user to input a number then we will ask the user to input a double number
Output
Enter a number
4227
183.38
183.38
So in this way you can take double as an input,to define a character we can define string,it takes an
array of characters
Program
package lesson1;
import java.util.scanner;
System.out.println(“Enter a string”);
string user_input_string=scan.nextLine();
System.out.print(“user_input_string”);
Output
Enter a string
So in this way you can take different types of input from the user,and assign it to a variable and then use
this variable-user_input_string in your program
So you can take scan. Then we see the suggestions as nextboolean ,next integer,nextfloat,nextbyte,so
whatever data type you want to take you can take so we see that you have seen the basics of scanning
In this chapter we will see what are math and arithemetic operators in Java,we can make a calculator
with this,so lets declare 2 variables with this
Int x , y
Int x , y , answer;
x=20;
y=30;
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
And now we will do some operations for x and y,we can add the values in the two variables x and y and
store the value in the third variable called answer
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
answer= x + y;
So when you add the two variables x and y and store it in the third variable or assign it to the third
variable answer then it will contain the result of the addition of the two variables in answer
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
answer= x + y;
System.out.println(“Answer= “ + answer);
So this + in the system.out.println is the concatenation operator it is used to contatenate two things the
string “Answer= “ and integer answer after the + ,so eclipse ide identifies it and gives the result
Output
Answer=50
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
answer= x - y;
System.out.println(“Answer= “ + answer);
Output
Answer=-10
Now to multiply these two variables we can do this
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
answer= x* y;
System.out.println(“Answer= “ + answer);
Output
Answer=600
MyClass.java
Package lesson 1;
Int x , y , answer;
x=20;
y=30;
answer= x / y;
System.out.println(“Answer= “ + answer);
Output=0
MyClass.java
Package lesson 1;
Int x , y , answer;
x=98;
y=30;
answer= x / y;
System.out.println(“Answer= “ + answer);
Output
Answer=3
We see here that we get the result only the integer values because x and y are integers and answer is
also an integer so we do not the get the remainder values of the division,to get the decimal value as the
answer,we can declare the variable as double
MyClass.java
Package lesson 1;
double x , y , answer;
x=98;
y=30;
answer= x / y;
System.out.println(“Answer= “ + answer);
Output
Answer=3.34
And if you declare the variables as integer you get the answer only as quotient and not the
remainder ,to get the decimal values you have to declare the variables as double
MyClass.java
Package lesson 1;
double x , y , answer;
x=98;
y=30;
answer= x % y;
// + - * / %
System.out.println(“Answer= “ + answer);
Output
Answer=8
We see that when we use % we get the remainder of the division of the variables,the remainder is 8 in
this case ,so these are the math operators functions
In this chapter we will see how to use increment operator,post increment and pre increment operator
and we wil also see assignment operator functions
MyClass.java
Package lesson 1;
int x=10;
System.out.println();
}
Suppose we initialize the value of x by 10,so we want to increment the value of x so we can do this by
x=x+1;
MyClass.java
Package lesson 1;
int x=98;
x=x+1;
System.out.println(x);
Output
99
MyClass.java
Package lesson 1;
int x=98;
x++;
System.out.println(x);
}
Output
99
So we can give x++ instead of x=x+1 and it will give the same result
MyClass.java
Package lesson 1;
int x=98;
x=x+1;
System.out.println(x++);
System.out.println(x);
Output
98
99
We see that the result is first 98 because we have given x++ in system.out.prinln so it will print the value
of x first and since ++ is post increment so sfter printing the value of x by 98 it will increment the value of
x by 1 so in the next statement the value of x will be 99
MyClass.java
Package lesson 1;
x=x+1;
System.out.println(++x);
System.out.println(x);
Output
99
99
In this case we are pre incrementing the values,so the for ++x it will pre increment the value of x in the
statement itself and display the result 99 and for the next statement it will print the value of x which will
same as the above
MyClass.java
Package lesson 1;
int x=90;
x+=8;//x=x+8
System.out.println(x);
}
Output
98
MyClass.java
Package lesson 1;
int x=90;
x*=10;//x=x*10
System.out.println(x);
Output
900
MyClass.java
Package lesson 1;
int x=900;
x/=10;//x=x/10
System.out.println(x);
}
Output
90
MyClass.java
Package lesson 1;
int x=90;
x-=10;//x=x-10
System.out.println(x);
Output
80
So the assignment operator is =,so in this way you can use increment operator and assignment operator
function in java
Day 8: IF Else Statements and Relational Operators Functions
In this chapter we will see what are conditional statements in java we will also see what are if
statements in java and else statements in java so first of all what are conditional statements in java,a
conditional statement is a statement which evaluates whether a condition is true or false and based
upon the condition we use a code,so for example,so we will take an example of if statements which is
also a conditional statement so how it works
MyClass.java
package lesson 1;
/*
If(boolean_expression)
*/
int x=10;
So for if statement we write if and inside these brackets we have a boolean condition which need to be
evaluated,and if the condition is true the it will excute the code inside these curly braces,otherwise it
will not execute the code if the condition is not true,so here we have x=10 and we need to evaluate if x
is really 10 or not,and based on this we want to execute a code or another statement
MyClass.java
package lesson 1;
/*
If(boolean_expression)
*/
int x=10;
if(x == 10){
So this == is comparison operator function it checks the equality of the if condition,so whwnver you
write this ==,we are checking if x is really =10 or not,and we have assigned above that x=10 so if this
statement is true
MyClass.java
package lesson 1;
/*
If(boolean_expression)
*/
int x=10;
if(x == 10){
system.out.println(“yes x==10”);
Output
Yes==10
This is true so in the condition if(x==10){ we replace the value of x by 10 and we are cheacking if 10==10
so this is true so it gives the result,now we give if x==20 and see the result
MyClass.java
package lesson 1;
/*
If(boolean_expression)
*/
int x=10;
if(x == 20){
system.out.println(“yes x==10”);
Output
It will not execute because the statement is false,we can use else statement to display this condition
MyClass.java
package lesson 1;
/*
If(boolean_expression)
*/
int x=10;
if(x == 20){
system.out.println(“yes x==10”);
else{
system.out.println(“no x!=10”);
}
}
Output
no x!=10
so if your condition is false your program goes to else statement and executes the code in the else
statement,so we have conditional operators as these
MyClass.java
package lesson 1;
/*
== is equal to
!= is not equal to
*/
int x=10;
if(x != 20){
system.out.println(“yes x==10”);
else{
system.out.println(“no x!=10”);
Output
yes x==10
similarly you can check other conditions here this from above in this
if(x != 20){
if(x >20){
if( x<20){
if(x>=20){
if(x<=20){
so these are the comparison operators function and we can use if else statements in java which is also
the conditional statements in java
In the last chapter we have seen how to use if statements in java and we have seen that how to use
relational operators functions inside the if statements,in this chapter we will see how to use logical
operators functions with if statements,so what are logical operators functions,so for example you want
to evaluate more than 1 condition using a single if statement so how you can do this
MyClass.java
package lesson 1;
Output
MyClass.java
package lesson 1;
}else{
}
MyClass.java
package lesson 1;
}else{
Output
So the and operator checks if both the condition is true then only it executes the if condition otherwise
it executes the else condition ,so here 1 condition was false so it executes the else condition,but in or
condition even if 1 condition is false it executes the if condition because it checks the condition 1 or 2 so
it executes the if statements
MyClass.java
package lesson 1;
}else{
Output
If both the conditions are false then it will execute the else statements
In the last chapter we have seen how to use if else statements which are condition based statements,in
this chapter we will see another conditional statements in java called switch statements,so whenever
you want to make multiple conditional checking we can use switch statements,we can use else
statements but generally in switch case we have to check multiple expressions
MyClass.java
package lesson 1;
int marks=90;
// byte , short , int or char
switch()
In the switch() brackets we evaluate the expressions,these expressions can be byte,short,int or char
We take switch(marks)
And based on this marks we can define cases inside the switch condition
MyClass.java
package lesson 1;
int marks=90;
switch(marks)
Case 90:
System.out.println(“excellent”);
break;
Case 60:
System.out.println(“good”);
break;
Case 40:
System.out.println(“ok”);
break;
}
Here we see that we will use this case 90 it will execute this statement then we will break out of this
switch statement and we will go to other cases,and if all the conditions are not met then we can define a
default condition,this condition will catch any other condition other than defined by these switch
conditions
MyClass.java
package lesson 1;
int marks=90;
switch(marks)
Case 90:
System.out.println(“excellent”);
break;
Case 60:
System.out.println(“good”);
break;
Case 40:
System.out.println(“ok”);
break;
default:
Output
Excellent
This is because we have defined marks as 90 and this variable marks we are evaluating with this switch
statement, switch(marks)
Then the switch statements sees the case,if the case is 90 then it executes this statement in 90,whatever
you write after this colon in case 90: and before the break any thing written will be executed and then
the statement will go out of the switch statements,so when you want to check multiple conditions or
cases and want to execute the same code you can do like this
MyClass.java
package lesson 1;
int marks=100;
switch(marks)
Case 100:
Case 90:
System.out.println(“excellent”);
break;
Case 60:
System.out.println(“good”);
break;
Case 40:
System.out.println(“ok”);
break;
default:
break;
Output
Excellent
So it gives here excellent,because in case 100 we did not give any break so after checkinh the condition
it goes to case 90 and then executes the statement of case 90 and then it sees the break and breaks out
of the switch case and prints excellent
MyClass.java
package lesson 1;
int marks=98;
switch(marks)
{
Case 100:
Case 98:
Case 90:
System.out.println(“excellent”);
break;
Case 60:
System.out.println(“good”);
break;
Case 40:
System.out.println(“ok”);
break;
default:
break;
Output
Excellent
In this it is evaluating three conditions in cases 100,98 and 90 and printing 1 statement that is excellent
which is there for the 3 cases it prints excellent and then break out of the switch statements
MyClass.java
package lesson 1;
public class MyClass{
int marks=80;
switch(marks)
Case 100:
Case 98:
Case 90:
System.out.println(“excellent”);
break;
Case 80:
Case 60:
System.out.println(“good”);
break;
Case 40:
System.out.println(“ok”);
break;
default:
break;
Output
Good
So in this way you can check the conditions in the switch statements
So a loop is a piece of code or statement which execute the block of code again and again until a
condition is met,suppose you want to execute a block of code again and again without rewriting it then
it will do this,we initialize an intiger a by 0 and we see what is the basic syntax of while loop
MyClass.java
package lesson 1;
int a=0;
while(a<10)
System.out.println(a);
a++;
So when the condition defined in the while in brackets () is true then this code will execute otherwise it
will break out of the loop
Output
3
4
So this system.outprintln executes the condition again and again until this condition a<10 is met,so
initially the value of a was 0 so it checks the condition is a<10 that is is 0<10 the condition is true so it
prints the value of a as 0,so now it increments the value of a a++ and now the value of a is 1 so it goes to
the while loop and checks the condition if a <10 that is if 1<10 so the condition is true so it prints the
value of a as 1 and it increments the value of I by 1 so the value of 1 is 2 ,now it goes to the while loops
and checks the condition if a<10 so 2<10 the condition is true so it prints the value of a as 2 and then
increments the value of a by 1 so now the value of a is 3 so now it goes to thye while loop and checks
the condition if a<10 that is if 3<10 the condition is true so it prints the value of a as 3 and increments
the value of a by 1 similarly it prints the value 4,5,6,7,8 and 9 and when a=10,it checks the condition in
the wile loop ifa<10 that is if 10<10 the condition is false and it breaks out of the while loop and prints
the following statements,if we want to include 10 also in the statement then we have to give the
condition a<=10
MyClass.java
package lesson 1;
int a=0;
while(a<=10)
System.out.println(a);
a++;
}
Output
10
MyClass.java
package lesson 1;
int a=10;
while(a>=1)
System.out.println(a);
a--;
10
So it gives the value similar to the condition until it is true and breaks out of the while loop when the
condition is false
In this chapter we will see what is do while loop,we have seen what is while loop and do while loop is
different from while loop we will see this
MyClass.java
package lesson 1;
int a=0;
while(a<=10)
System.out.println(a);
a++;
}
System.out.println(“---------------------------------------------------------------------------“);
Int b=0;
do{
system.out.println(b);
b++;
}while(b<=10);
We will see the difference between while loop and do while loop in this program this
Output:
10
------------------------------------------------------------------------------------------------------------
1
2
10
the while loop first evaluates the condition and then executes the statement
and the do while loop executes the statement first and then evaluates the condition fo example
MyClass.java
package lesson 1;
int a=0;
while(a<=-1)
System.out.println(a);
a++;
System.out.println(“---------------------------------------------------------------------------“);
Int b=0;
do{
system.out.println(b);
b++;
}while(b<=-1);
Suppose we change the condition of while loop and check the statement then we see that the output is
this
Output
----------------------------------------------------------------------------------------------
We get this output because the while loop checks the condition first and it checks whether a<=-1 but
here a is 0 and 0 !<=-1 so the condition is false so it does not executes and goes to the do while loop so
the do while loop executes first before evaluating the condition it prints a=0 then checks the condition
that 0!<=-1 and it goes out of the loop so the while loop does not execute the statement if the
statement is false but the do while loop executes the statement even if the statement if flase and it
executes atleast once even if the statement is false,so this is the difference between while loop and do
while loop
In this chapter we will see what are arrays and how we can declare arrays in java,so arrays are same as
variables but it can store more than 1 values at a time,but the condition is that you can store only same
kind of values in the array,suppose you want to staore 10 integers in an array you can do this but if you
want to store 5 integers and 5 double values you cannot do it because you can store only same kind of
data type in array,so let us see how we can declare an array,so you need to only give the data type for
example you want to give the integer array in the data type and then we use these square brackets
Int[ ]myintarray={}
Int[ ]myintarray={1 , 3 , 2, 4 , 5}
MyClass.java
Package lesson 1;
So you can store multiple integers in the arrays now we have 5 integers
You can also declare arrays like this declare the data type
MyClass.java
Package lesson 1;
So the other way is not the preffered way of declaring arrays so we give it in comments
MyClass.java
Package lesson 1;
/*
Int[ ] myIntArray=newint[3];
*/
We will see these later now we are using the type of declaration as shown above
and ={1 , 3 , 2 , 4 , 5} are called the elements of the array,and the index of the position at where these
values are stored are called index
MyClass.java
Package lesson 1;
/*
Int[ ] myIntArray=newint[3];
*/
System.out.println(myintarray[ 0 ]};
Suppose you want to access the array element 1 so we can call in system.out.println the index of the
array so myintarray[ 0 ] and if you want to access the element you can call in system.out.println the
index of the array so myintarray[ 3 ]
Output
MyClass.java
Package lesson 1;
/*
Int[ ] myIntArray=newint[3];
*/
System.out.println(myintarray[ 3 ]};
Output
Suppose you want to print all the elements of the array we use while loop
MyClass.java
Package lesson 1;
Int[ ] myIntArray=newint[3];
*/
Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
output
100
38
13
48
55
In the previous chapter we have seen how to use arrays in java and in this chapter,we will see how we
can use for loop effectively,we use this,for loop is same as while loop or do while loop in the way that
we write the same code again and again until some condition is met,but here we have more control on
the condition so lets see what is the basic declaration of for loop,we write for
Program:
package lesson 1;
int[] myintarray={100,31,26,48,52};
for()
Program:
package lesson 1;
int[] myintarray={100,31,26,48,52};
Here we have 3 conditions called initialization,expression and increment,inside the for loop we can
define the integer value this
Program:
package lesson 1;
int[] myintarray={100,31,26,48,52};
MyClass.java
Package lesson 1;
/*
Int[ ] myIntArray=newint[3];
*/
Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
}
In the last chapter we have seen how to use arrays and print all the statements in the while loops using
arrays and we have declared this,index=0 and this declaration we have done outside also the loop which
was while loop,in for loop we can initialize the index inside this expression,index is 0 index<5 and the
third condition is this,in for loop we can increment the index inside this expression itself
MyClass.java
Package lesson 1;
/* Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
}*/
System.out.println(myintarray[index]);
So the while loop serves three purpose the initialization which you were doing outside when you were
using while loop and then the expression which will give you true or false,and the increment while you
can do inside the while loop,so we see the output this
100
31
26
48
52
So how this works so the for loop first initializes the variable this,and then it chaecks the condition if the
index is less than 5 because we have initialized the index by 0,so 0 is <5 so then it executes the
statement,so it increments the value first initialization and then if the condition is true it will print the
value and then it will increment the index value by 1,so the value of array at index 0 is 100,and then it
will increment the index of array by 1 which is this,so at index 1 the value of 1 <5 so it prints the value of
array at index 1 which is 31,so now the index of array is 2 so 2<5 and then it will print the value now the
inxex value is 3 so 3<5 so it will print the value of the array,so similarly it will print the value for 4 and for
5 it will come out of the loop this is the value of array
MyClass.java
Package lesson 1;
/* Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
}*/
System.out.println(myintarray[index]);
For()
{
}
So declaration is the same but in case of array you can directly use this array here this
So this is the name of the variable which will hold the value 1 by 1,we will give colon here this and then
we will give the name of the array
MyClass.java
Package lesson 1;
/* Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
}*/
System.out.println(myintarray[index]);
{
So what this for expression is doing is that
It will go on the array 1 by 1 and it will store the value of the array 1 by 1 so the first value is the value
saved in this element,the value is 100 then the value is 38,13,48 and 55 so it will iterate the array 1 by
1,it will transfer the value of the array in this element and then you can access the value of the element
like this
MyClass.java
Package lesson 1;
/* Int index=0;
while (index<5){
System.out.println(myintarray[ index]);
Index++;
}*/
System.out.println(myintarray[index]);
}*/
System.out.println(myintarray[index]);
}
we see the output
output:
100
38
13
48
55
Array will transfer the element value 1 by 1 and then you can access this element value 1 by 1 when,this
is a good way of iterating the values on for loop using arrays
String is a sequence of characters or in other words,string is a character array,so how you can use
strings,you have to use the keyword string,and you can name your string whatever you want,in quotes
whatever string you need like “Hello World” this string you can print using systemout or print
ln,system.out.println there are a few properties related to strings and then
Program:
Package lesson 1;
System.out.println(mystring);
Package lesson 1;
System.out.println(mystring);
You can give the variable length of the string and length,we will return a size or length of the string,
Program:
Package lesson 1;
System.out.println(mystringLength);
Output
13
Now for example the length function,modified string we will declare as this
Program:
Package lesson 1;
Public class MyClass{
String mystringinLowerCase=myString.toLowerCase();
System.out.println(mystringin LowerCase);
Output
hello world
Program:
Package lesson 1;
String mystringinLowerCase=myString.toUpperCase();
System.out.println(mystringinCase);
Output
HELLO WORLD
Program:
Package lesson 1;
String mystringinLowerCase=myString.toUpperCase();
System.out.println(mystringinCase);
Output
HELLOWORLD
Program:
Package lesson 1;
String mystringinLowerCase=myString.toUpperCase();
System.out.println(mystring.replace(‘e’ , ‘a’));
}
If you have to replace the string e by a then we can do this
Program:
Package lesson 1;
String mystringinLowerCase=myString.toUpperCase();
System.out.println(mystring.replace(‘e’ , ‘a’));
This is how you can replace strings in java,there are other functions
System.out.println(mystring.indexOf(‘l’));
Output
In this chapter we will see what is method and how we can use methods in java,methods are also called
functions in other languages,if you use functions or methods they can be used interchangeably,but in
java the method keyword is preffered so what is a method,a method is the piece of code that executes a
logic,and you can wrap with the methods with the name and you can call this,so how you can use a
method,so first of all,main is also a method that this java program has this we have seen in this,so in this
method java is expecting us to provide
Program:
Package lesson1;
There can be only 1 main method in the java program,but other than main,there is a reserved name in
java,you can provide any name,we will first give to access specifier in this method,we will see this public
and static,we will see this public and static in other chapters when we see how to use classes,then we
will start the return value and then comes this myFirstMethod
Program:
Package lesson1;
So here the public static is access specifier and the public keyword tells java that that it is accessible
anywhere inside the class and void is the return type,right now we are not returning anything,but we
will show in the further chapter how to return value using method,and then we will see more options
except void and then comes the method name and in this
Program:
Package lesson1;
In the circular bracket comes the argument now we are not passing the arguments and in these curly
braces,for example we want to print a message,system.out.println and then we write hello youtube,
This is a basic syntax of code,and this is the basic code which you want to write or access and you can
call this method in your main method here this and you don’t need to execute or print anything you call
it as it is,you need to copy the name of the method
Program:
Package lesson1;
myFirstMethod();
System.out.println(“Hello Youtube”);
Output
Hello Youtube
So it is not directly printing in the main method but it is calling the function,advantage of using this
function or code is that it will make your code,it will make your code readable and consise and it will
make your code look ok this,and you can call your method again and again in your main method and
without rewriting your code
Program:
Package lesson1;
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
System.out.println(“Hello Youtube”);
So the basic rule is when you want to call the method,so when you have this code to write again and
again then you may have this put inside this in a method and then you can make lesser lines of code
then more lines of code,so these code contains 5 lines and we
Program:
Package lesson1;
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
So this here we have this 5 lines of code,so we don’t need to write these 5 lines of code again and
again,so if we write these 5 lines,and if have to do this without the code then we can do these
Program:
Package lesson1;
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
So when the methods uses this code we cant read the code to be able to read the code,so we will
execute method by a single line of code
Program:
Package lesson1;
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
myFirstMethod();
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
System.out.println(“Hello Youtube”);
You can give logical name to your method so that it is understandable and then,so this is the basic type
of methods in java there are other methods like methods that take an argument and methods that
returns the value function
In the previous chapter we have seen how to declare a method in java,we will see what is a method and
why it is used,in this chapter we will see,we will see a few more properties we can add to methods,1
thing we will see is passing parameters to the method andf the other is called the returning values of the
method,so this is a method we have seen in the previous chapter
Program:
Package lesson1;
sayHello();
System.out.println(“Hello Youtube”)
But this does not serve the purpose it prints hello you tube,to serve the purpose you can add an
argument to this function, sayHello( ){
So public static is the access specifier,void is the return type which does not return anything,sayHello is
the function,and we will pass an argument to the function,so we pass the argument and in the curly
braces we write the statement,suppose we want to read a few persons based on their name,so we will
pass argument to the function,we will pass name in the form of string,so we first give the data type
sayHello(string ){
sayHello(string name ){
and then the value or variable name we need to pass,so this type of declaration inside the round bracket
is this,is called the parameter passing,and this parameter string inside the function string you can use
inside this function
system.out.println(“Hello” + name);
Program:
Package lesson1;
sayHello();
System.out.println(“Hello” + name)
Program:
Package lesson1;
System.out.println(“Hello” + name)
Program:
Package lesson1;
sayHello(“Micheal”);
sayHello(“Mark”);
sayHello(“Micheal tyt”);
System.out.println(“Hello” + name)
Output
Hello Micheal
Hello Mark
Program:
Package lesson1;
sayHello(“Micheal”);
sayHello(“Mark”);
sayHello(“Micheal tyt”);
System.out.println(“Hello” + name)
And in this we can pass multiple values whatever we need to add like this
System.out.println(a + b);
We can add these values calling a+b,what what this will do is it will take the values of a and b in int a and
int b and return the result
Program:
Package lesson1;
Add(100,204);
System.out.println(“Hello” + name)
System.out.println(a + b);
Output
304
So using a same function you can operate on different values and functions
Program:
Package lesson1;
Add(100,204);
Add(300,204);
Add(500,204);
}
System.out.println(“Hello” + name)
System.out.println(a + b);
Output
304
504
704
So this makes your code understandable,you can add perimeters here this
Program:
Package lesson1;
Add(100,204);
Add(300,204);
Add(500,204);
System.out.println(a + b+c);
You can add different parameters to the function like a,b and c
Program:
Package lesson1;
Add(100,204+100);
Add(300,204+500);
Add(500,304,500);
System.out.println(“Hello” + name)
System.out.println(a + b+c);
}
Output
304
1004
1304
You need to give the data type of the variable and the variables that will hold the values this,this is
called parameter passing,we can only pass the values,but if we want to access the values this,using
parameter passing we cannot pass the result using this values this,how we can use main function to do
another arithemetic operation,so how we can do this,so using this function it is not possiblke but thesre
is a way to do this is this,is by returning few values this,so we can simplify the add function,so how we
can do this,but when we want to use the result inside the main function,we return integer values
this,and this return you tell what kind of value you want to return from the function,and the same it will
return from here this,we will write return a+b+c,so you are taking all the values which you pass to this
function, Public static void Add( int a , int b, int c)
And you are adding this and returning this to this function,so we
Program:
Package lesson1;
Add(100,204+100);
Add(300,204+500);
Add(500,304,500);
System.out.println(“Hello” + name)
//System.out.println(a + b+c);
return(a+b+c);
We are passing these 3 values this and return the return,whenever a function is returning anything,you
can store it in another variables 1 of the same type,so the data type of the returning and the data type
in which you need to store in this
Program:
Package lesson1;
int sum=Add(500,304+500);
System.out.println(“Hello” + name)
//System.out.println(a + b+c);
return(a+b+c);
So in this int sum=Add(500,304+500); what it will do is that 500 will be replaced by a,304 will be
replaced by b and 500 will be replaced by c and it will add the value and return the result,and you can
store this sum of the return in another variable this, int sum=Add(500,304+500); sum variable this,and if
you want to do another arithemetic operation on this sum
Program:
Package lesson1;
int sum=Add(500,304+500);
System.out.println(“Hello” + name);
//System.out.println(a + b+c);
return(a+b+c);
Program:
Package lesson1;
int sum=Add(500,304+500);
int result=sum*16;
system.out.println(result);
System.out.println(“Hello” + name);
}
Public static void Add( int a , int b, int c)
//System.out.println(a + b+c);
return(a+b+c);
Then you can multiply these sum and do arithemetic operations,and then you can store this result in
another variable this,so in this way the function or method,you can make the code organized and
consize,and the code is understandable,so we have seen the basic concept of passing parameters,and
returning values in the same way we can make any logic
We can do the multiplication of numbers,the division of numbers,you can subtract numbers or any
arithemetic,so anything you want to do,you can do inside the function,youi can take the arguments or
parameters here and you can return the result,so that you can use it in the main method,so we have
seen this passing parameters form a method and returning values from a method
Till now we have seen the procedural part of java,now from this chapter we will see the object oriented
aspects of java,in this chapter we will see what are classes in java and how we can declare classes in java
and how we can use classes in java,so ehat is classes in java,we have noticed that whenever we have
created our firat program,we had a class function and inside this class we were calling this main method
MyClass.java
Package lesson 1;
}
so every java class has 1 class and 1 main method,and class is basically a data structure or type inside
which we can define a few variables called member variable functionsand methods which are member
methods,and the most important properties of classes is that,we can create instance or object from
class,we can maintain different stages of this class using these instances or objects of the class,so let’s
see how we can define a class and how we can use class,for example we need to take a student data
structure and for that we want to take
so here this
Package Explorer
FirstProject
Src
MyClass.java
Java Class
Package :lesson 1
Name: Student
Click ok
Package Explorer
FirstProject
Src
Lesson 1(right click on this package)-class
MyClass.java
Student.java
Student.java
Package lesson 1;
This class can heve member variables and methods,so what can be the traits of a student,a
student can have the id,student can have the variable function name and student can have the variable
function age
Student.java
Package lesson 1;
Int id;
String name;
Int age;
So we have created a class and it can have member variables functions,we also have member
methods which we will also see in this,so we will move it to the basic student class,it has id,name and
age,we can call this class,public class student,from student.java to main method in myclass.java
MyClass.java
Package lesson 1;
Lets create an instance of or object or the class,just as we give name to the variable in
student.java we give the name to the variable function student
MyClass.java
Package lesson 1;
So this new student we have created an instance of the class Student,we name the student class
as mark,marke is called the object or instance,and this object we can use to assign values to the member
variables,for example,so mark id is whenever you call this instance mark
MyClass.java
Package lesson 1;
Mark.id=1;
Mark.name=”Mark”;
Mark.age=20;
}
}
So what we have done is we have taken the instance of the class student as mark,and with this insyance
mark we have assigned a few values functions in called id,name,age,we can access these values from
this
MyClass.java
Package lesson 1;
Mark.id=1;
Mark.name=”Mark”;
Mark.age=20;
Output
MyClass.java
Package lesson 1;
Mark.id=1;
Mark.name=”Mark”;
Mark.age=20;
Micheal.id=1;
Micheal.name=”Micheal”;
Micheal.age=22;
Output:
The member values that are independent of this instance,we have object values or object member
values which are independent of this instance that is micheal’s id,micheals name and micheals age is
independent of mark’s id,marks’s name and mark’s age,in java programming the direct use of variables
functions are not ok this because any other person or whoever is using your code can modify these
values this,so we use a few functions or methods that can get the values functions and you can set these
values function then what is getting and setting the values functions is that,so what we do by getting and
setting the values functions is that,we can go to student.java and then in this go to this
Student.java
Package lesson 1;
public class student{
Int id;
String name;
Int age;
So click on this variable function int id;select id then source-generate getters and setters
age
Id
getid()
setidint()
name
age
id
name
we want to set getters and setters for these variables functions because we don’t want to access these
values directly
Student.java
package lesson 1;
int id;
this .id;
return name;
this.name=name;
return age;
this .age=age;
String name;
Int age;
So whenever we need to assign a value to the method set id; we use set method and whenever we need
to get the value to the method we use get value,so we don’t want to give access to another user for
this,now we need to assign a value to this mark instance this
MyClass.java
Package lesson 1;
Mark.setId(1);
Mark.setName(“Mark”);
Mark.setAge(20);
Micheal.setid(3)
Micheal.setName(”Micheal”);
Micheal.setAge(22);
Output
In the previous chapter we have seen how we can declare a class,and how we can declare the instance
of a class,and in this chapter we will see class constructor and what is a class constructor,we will be
seeing what is a class constructor
Create-file-new class
Source folder:Firstproject/src
Package lesson 1
Name-cube
-ok enter
cube.java
package lesson 1;
int length;
int breadth;
int height;
return(length*breadth*height);
We have declared the class so we will initialize an instance of this class in MyClass.java
MyClass.java
package lesson 1;
}
This new Cube(); is the constructor of the cube class
The property of a constructor is even if you don’t define a constructor,java will create a default
constructors for the class,we can create a constructor for us,the constructor name is this,the constructor
name is exactly the same as class name,so for defining a constructor we can write this
cube.java
package lesson 1;
public class Cube{ /*constructor has the same name as the class and does not return any value
fucntion*/
int length;
int breadth;
int height;
return(length*breadth*height);
Cube(){
We are not providing values function here in cube() but we can provide parameters in this,the
properties of constructor is it has the same name as class,and in myclass we have defined as the
instance of the new class
Output
We are in a constructor
So whenever we create an instance of the class,so the first method that is initialized is a constructor,so
constructor is generally used initialized
cube.java
package lesson 1;
public class Cube{ /*constructor has the same name as the class and does not return any value
fucntion*/
int length;
int breadth;
int height;
return(length*breadth*height);
Cube(){
Length=10;
Breadth=10;
Height=20;
So if the user does not initialize the length,breadth and height java will initialize this
MyClass.java
package lesson 1;
}
We will use the instance of the cube class
System.out.println(cube1.getcubeVolume());
MyClass.java
package lesson 1;
System.out.println(cube1.getcubeVolume());
We can provide another constructor called constructor overloading function,we have 1 constructor
cube() with length,breadth and height as 10,10,20,and we can provide another constructor
cube.java
package lesson 1;
public class Cube{ /*constructor has the same name as the class and does not return any value
function*/
int length;
int breadth;
int height;
return(length*breadth*height);
Cube(){
Length=10;
Breadth=10;
Height=20;
Length=l;
Breadth=b;
Height=h;
This is constructor overloading function,we can define different constructor which does not take an
argument,and which takes an argument and we declare the l,b and height in the variables functions,the
previous is the default constructor which contains the values functions,and the constructor for which
you want to pass the values functions you can use the another constructor,so what we can do is we can
define another instance of the cube class in MyClass.java
MyClass.java
package lesson 1;
System.out.println(cube1.getcubeVolume());
System.out.println(cube3.getcubeVolume());
}
And we can call the cube.getcubevolume method,we pass the l,b,h as 20,20,20 in myclass.java so java
will understand that this is for this constructor in cube.java the another constructor,you can click on this
Click on Cube(); and it will bring you to this default constructor in cube.java
Output
6000
8000
So constructor is the member method which has the same name as the class,constructor does not
return a value function,constructors can not take a parameter and can take a parameter,we can define
multiple constructor and use them with class,we have defined a constructor with cube1 and constructor
with 3 parameters,this way you can use constructor in java
In the previous chapter we have seen how to use a method,so method overloading is the same thing as
defining method ,you can define method,you can define different method with the same name,that will
do different logic,suppose I want to add,we will declare a method for this
MyClass.java
Package lesson 1;
Return(a+b );
So we pass int a and int b as parameters,we return the sum of a and b,here we are not declaring the
sum before but we may need to add double,decimal or string values functions,we can use(a+b) as a
concatenation operator function,so in java we can define two different method with the same name,but
different parameters and different arguments
MyClass.java
package lesson 1;
return(a+b );
return(a+b );
return(a+b );
This is called method overloading,we define different methods with the same name,to perform a
different logic but they have the same name,to perform the same task,but the data type you need to
pass or parameters you need to pass is different,and whenever you will call this method,and whwnever
you need to add two values functions you can say
system.out.println(Add(1,36));
MyClass.java
package lesson 1;
public class MyClass{
system.out.println(Add(1,98));
system.out.println(Add(5.045,36.898));
system.out.println(Add(“Hello”, “World”));
return(a+b );
return(a+b );
return(a+b );
So by default java knows that whenever you pass two integers(1,36) then you are calling this method
return(a+b );
So the program knows which function you are calling for decimal values argument(5.045,36.898)we are
calling double function and for string values arguments(“Hello” ,”World”),we are calling the string
function
Output
99
40.35678
Hello World
So in the first case this Add(1,98) is calling this int function and similarly the add for double is calling the
double function and the add for string is calling the string function,so in method overloading we define
different methods with the same name,to perform different operations this,but logically they will be
doing same kind of work,so this is method overloading in java
In this chapter we will see what is a static keyword in java,so what does a static keyword do in a class,we
have seen the use of static keyword with member method or member function of a class,so what is the
meaning of static keyword,the meaning of static keyword is this,so whenever you declare a member as
static,a static member belongs to a class,instead of a specific instance of a class
MyClass.java
Package lesson 1;
Hello.java
package lesson 1;
return message;
It contains two methods 1 is a static method and 1 is a non static method,in the first method we have
added static,so it is taking string as an argument and it is returning,and it is returning whatever we pass
and the same method,the other method has function DoathingElse,and this method does not have the
static keyword,so whenever we declare an instance of a class for example,this the static member is
shared by this,whenever we create an instance of a class we can create many instances of a class,so
static member is shared by every instance this program
MyClass.java
Package lesson 1;
The static member method is shared by every instance,and the static members only belong to class and
not.it will not belong to the instance of the class but it will only belong to the class itself,so if you do a
thing like this
Hello.Doathing(message)
}
Class.java
package lesson 1;
return message;
return message;
MyClass.java
Package lesson 1;
Hello.Doathing(“Hi Youtube”);
MyClass.java
Package lesson 1;
Hello.Doathing(“Hi Youtube”);
So when you declare this method the staic method will be gone,but a non static method
Cannot be accessed directly from a class,but when you add static to the method name or variable
function name,then doathing is accessed from the class and doathing else is not a static method cannot
be accessed directly by class
return message;
But not static method is available and cann be accessed from the instance of the class
MyClass.java
Package lesson 1;
static method should be accessed using the class itself,this is the correct way of using static method,you
can declare a variable function as static
Class.java
package lesson 1;
return message;
return message;
And then whenever you want to access this age from Myclass.java,it is showing error that static age
should be accessed statically,we take another instance of this class hello and we take this
MyClass.java
Package lesson 1;
hello1.age=50;
system.out.println(hello1.age);
We assign a different value this hello1.age and we will print the age again this
MyClass.java
Package lesson 1;
system.out.println(hello1.age);
Here the instance hello1 is thebmember of the class Hello,so we declare the instance as the class itself
hello1.age,so whenever we want to declare a variable or method static,we can just access it using the
class,and whenever we are using non static method or variable function we can access it as shown
above this
In the previous chapter we have seen how to use static keyword in java class,we will give more example
so the meaning of static will be more understood properly,suppose we have a class and in this class we
have 3 member variables functions,1 is the name of the student and 1 is the age of the student,we have
the getter and setter functions of this
Student.java
Package lesson 1;
String name;
Int age;
return name;
this.name=name;
return age;
We can make this getters and setters,in source-we go to this-generate getters and setters,and then
select all then the getters and setters will be generated,we can start with this program and generate
getters and setters
Student.java
Package lesson 1;
String name;
Int age;
Student.java
Package lesson 1;
String name;
Int age;
return name;
this.name=name;
return age;
this.age=age;
}
Student.java
Package lesson 1;
String name;
Int age;
Student(){
NoOfStudents
return name;
this.name=name;
return age;
this.age=age;
We want to keep record of the number of students so we declare it as static,it can be shared by all the
instances,now we define a constructor whose name is the name of the class student,so here whatever
constructor is called we want to increase the number of constructor by 1,so whenever
static int NoofStudents=0;
Student(){
NoOfStudents++;
Wew access this value this,we will take the function to get the no of values of student
Student(){
NoOfStudents++;
return NoOfStudents;
Student.java
Package lesson 1;
String name;
Int age;
Student(){
NoOfStudents++;
return NoOfStudents;
}
}
return name;
this.name=name;
return age;
this.age=age;
So now we will go to the student class and define a few instances of the student class from Student.java
so we go to this MyClass.java
MyClass.java
package lesson 1;