Chapter02 Java Input & Output
Chapter02 Java Input & Output
Chapter02 Java Input & Output
com
+91 9582815419
Chapter 02
Java Input & Output
1 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Contents
Java Platform Independent ........................................................................................................................... 3
Java is platform independent and C/C++ are not: .................................................................................... 3
Difference between JDK, JRE and JVM ......................................................................................................... 4
JDK:............................................................................................................................................................ 4
JRE: ............................................................................................................................................................ 4
JVM: .......................................................................................................................................................... 4
Program to display some output: ................................................................................................................. 5
Understanding public static void main (String [] args) in Java ...................................................................... 5
Setting class path of java............................................................................................................................... 7
Setting the classpath ................................................................................................................................. 9
Java Input/Output ......................................................................................................................................... 9
Java Output : ............................................................................................................................................. 9
print() .................................................................................................................................................... 9
println() ................................................................................................................................................. 9
Java Input : ................................................................................................................................................ 9
2 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Java compiler
javac HelloWorld.java
Byte code
HelloWorld.class
Native code
When Java program is compiled it generates “Bytecode” not a “native code”. When we run the
bytecode, it is converted to native code and then it is executed.
Native code generated from bytecode is temporary and it is not stored in any file.
If we want to run the java program on other OS, just move the bytecode and run again, when bytecode
is run it is converted to native code again and then executed, so java program is platform intendent.
3 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
JVM:
JVM – Java Virtual machine (JVM) is a very important part of both JDK and JRE because it is contained or
inbuilt in both. Whatever Java program you run using JRE or JDK goes into JVM and JVM is responsible
for executing the java program line by line hence it is also known as interpreter.
4 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
In Java programs, the point from where the program starts its execution or simply the entry point of Java
programs is the main () method.
Every word in the public static void main statement has got a meaning to the JVM.
Public: It is an Access modifier, which specifies from where and who can access the method. Making
the main () method public makes it globally available. It is made public so that JVM can invoke it from
outside the class as it is not present in the current class.
Static: It is a keyword which is when associated with a method, makes it a class related method.
The main () method is static so that JVM can invoke it without instantiating the class. This also saves the
unnecessary wastage of memory which would have been used by the object declared only for calling
the main() method by the JVM.
Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method
doesn’t return anything, its return type is void. As soon as the main() method terminates, the java
program terminates too. Hence, it doesn’t make any sense to return from main() method as JVM can’t
do anything with the return value of it.
5 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point
of the java program. It’s not a keyword.
String[] args: It stores Java command line arguments and is an array of type java.lang.String class. Here,
the name of the String array is args but it is not fixed and user can use any name in place of it.
Apart from the above mentioned signature of main, you could use public static void main(String
args[]) or public static void main(String… args) to call the main function in java.
6 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
package com.yuktisoftwares.p1;
}
}
To compile:
D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>javac -d . Hello.java
D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>java com.yuktisoftwares.p1.Hello
If we change the package name inside the file, then compiled file would be at different location:
Example:
7 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
package abc.def.xyz.p2;
}
}
To compile:
D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>javac -d . Hello.java
package com.yuktisoftwares.p1;
void m1() {
System.out.println("m1");
}
}
}
D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>java
com.yuktisoftwares.p1.Hello➔Error
8 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>java
com.yuktisoftwares.p1.Hello➔Output
Java Input/Output
Java Output : To send output to standard output(screen) are :
System.out.println(); or
System.out.print();
System is a class.
out is a public static field: it accepts output data.
Java Input : Java provides different ways to get input from the user. Scanner is a class which is used to
take input from the user.
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
9 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
10 www.yuktisoftwares.com
+91 9582815419