Notes Lec2-3050
Notes Lec2-3050
First Program
a) About Eclipse
A new Java class can be created using the New Java Class wizard. The Java Class
wizard can be invoked in different ways
1. By clicking on the File menu and selecting New Class, or
2. By right clicking in the package explorer and selecting New Class, or
3. By clicking on the class drop down button and selecting class.
Note : We will understand what classes are when we will study Object Oriented
Programming. For now you can assume them as a file. Also name of class and
.java file inside which we have this class should be same.
b) About Main
1. This is the line at which the program will begin executing. This
statement is similar to start block in flowcharts. All Java programs begin
execution by calling main()
2. We will understand what public, static, void mean in subsequent
lectures. For now we should assume that we have to write main as it is.
3. The curly braces {} indicate start and end of main.
c) print / println
Output:
Hello World
Programming is fun
Variables
a) Add two numbers
Output:
15
Here, we used variables to store values of two integers and their sum. Thus, a
variable is a basic unit of storage in a Java program.
While writing variable names you should be careful and follow the rules for
naming them. Following are the rules for writing variable names -
1. All variable names may contain uppercase and lowercase letters (a- z, A- Z),
underscore ( _ ), dollar sign ($) and the digits 0 to 9. The dollar sign
character is not intended for general use. No spaces and no other special
characters are allowed.
2. The variable names must not begin with a number.
3. Java is case- sensitive. Uppercase characters are distinct from lowercase
characters.
4. A Java keyword (reserved word) cannot be used as a variable name.
Based on the data type of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, we can store integers, decimals, or characters in
these variables.
Example Code:
Output:
Simple Interest = 750.0
Taking Input
a) Scanner
The Java Scanner class breaks the input into tokens using a delimiter that is
whitespace by default. It provides many ways to read and parse various
primitive values.
In order to use scanner you have to write this import statement at the top
import java.util.Scanner;
Example Code:
Sample Input:
10 5
Output:
15
Here, s.nextInt() scans and returns the next token as int. A token is part of
entered line that is separated from other tokens by space, tab or newline. So
Example Code:
import java.util.Scanner;
Sample Input:
2500.0 6.0 5.0
Output:
750.0
import java.util.Scanner;
public class ScannerDemo1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char ch = s.next().charAt(0); // character input
System.out.println("input character = " +ch);
}
}
Sample Input:
k
Output:
input character = k
Sample Input:
Coding Ninjas
Output:
Coding
Here, s.next() returns the next token as String. A token is part of entered line that
is separated from other tokens by space, tab or newline. So when input line is -
then s.next() returns the first token i.e.
METHOD DESCRIPTION
public String next() It returns the next token from the Scanner.
public String nextLine() It moves the Scanner position to the next line
and returns the value as a string.
public byte nextByte() It scans the next token as a byte.
public short nextShort() It scans the next token as a short value.
public int nextInt() It scans the next token as an int value.
public long nextLong() It scans the next token as a long value.
public float nextFloat() It scans the next token as a float value.
public double It scans the next token as a double value.
nextDouble()
Example code:
Sample Input:
100 Hello World
Output:
100
Hello World
Here, s.nextInt() scans and returns the next token as int. A token is part of
entered line that is separated from other tokens by space, tab or newline. So
when input line is -