Chapter02 Java Input & Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

www.yuktisoftwares.

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 Platform Independent

C, C++ Java Program


Programs
HelloWorld.java
Hello.c

Java compiler
javac HelloWorld.java

Byte code

HelloWorld.class

Executable code JVM


Native code java HelloWorld
Hello.exe

Native code

Java is platform independent and C/C++ are not:


When C program is compiled, it generates “native code”, which can be understood by current operating
system. If we move this native code to other OS, it can not be understood because “native code
representation changes from one OS to other OS. So, C/C++ are platform dependent.

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

Difference between JDK, JRE and JVM


JDK:
Java Development Kit (in short JDK) is Kit which provides the environment to develop and
execute(run) the Java program. JDK is a kit (or package) which includes two things
1. Development Tools (to provide an environment to develop your java programs)
2. JRE (to execute your java program).
Note: JDK is only used by Java Developers.
JRE:
It is an installation package which provides environment to only run (not develop) the java program (or
application) onto our machine. JRE is only used by them who only wants to run the Java Programs i.e.
end users of our system.

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.

JRE= JVM + Library Classes


JDK = JVM + Library Classes + Development Tools

4 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419

Program to display some output:


Filename: P2_Output.java
Go to file location:
javac P2_Output.java
java P2_Output
class P2_Output {
public static void main(String[] args) {

System.out.println("1. println ");


System.out.println("2. println ");

System.out.print("1. print ");


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

Understanding public static void main (String [] args) in Java


class HelloWorld {
public static void main(String args[]){
System.out.println("HelloWorld!!");
}
}

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.

public static int main(String[] args) {


System.out.println("HelloWorld");
return 0;
}

6 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419

Setting class path of java


We can compile in two ways:

1. javac -d D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15 Hello.java


2. javac -d . Hello.java

package com.yuktisoftwares.p1;

public class Hello {


void m1() {
System.out.println("m1");
}

public static void main(String[] args) {


Hello h = new Hello();
h.m1();

}
}
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;

public class Hello {


void m1() {
System.out.println("m1");
}

public static void main(String[] args) {


Hello h = new Hello();
h.m1();

}
}
To compile:

D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>javac -d . Hello.java

D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>java abc.def.xyz.p2.Hello➔Gives output

Change package name:

package com.yuktisoftwares.p1;

public class Hello {

void m1() {
System.out.println("m1");
}

public static void main(String[] args) {


Hello h = new Hello();
h.m1();

}
}

D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages> javac -d d:\abc\def Hello.java

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

Setting the classpath


D:\TRAINING\2020-KHUSHBOO-BATCH\LABS\Lab15_packages>set classpath=d:\abc\def;%classpath%

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.

print() - It prints string inside the quotes.


println() - It prints string inside the quotes similar like print() method. Then the cursor moves to the
beginning of the next line.

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.

To use Scanner we need to import "java.util.Scanner" package.

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

System.out.println("You entered " + number);


// closing the scanner object
input.close();
}
}

10 www.yuktisoftwares.com
+91 9582815419

You might also like