A. System Is A Class in The Java - Lang Package. B. Out Is A Static Member of The System Class, and Is An Instance of Java - Io.Printstream
A. System Is A Class in The Java - Lang Package. B. Out Is A Static Member of The System Class, and Is An Instance of Java - Io.Printstream
SIMPLE
SECURE
PORTABILITY
OBJECT-ORIENTED: Object-oriented programming organizes a program around its
data (that is, objects) and a set of well-defined interfaces to that data. An object-
e.
f.
g.
h.
i.
BIT DEPTH:
JVM spec 16
VALUE RANGE: T or F
8. Meaning
of
16
32
64
32
64
0 to 65535 -128to-127
--
System.out.println("Hello
class,
and
World!");
is
an
instance
a. public is the visibility. This can be public, private, protected or (if you omit a value)
default.
b. static is a special [optional] keyword that indicates that this method can be called
without creating an instance of this class. Without it, you have to instantiate this class and
call this method from the resulting object./ When the JVM makes call to the main method
there is not object existing for the class being called therefore it has to have static method to
allow invocation from class.
c. void is the return type of this method, indicating that this method doesn't return
anything. Methods must have a return type.
d. main( ... ) is the name of this method. Methods have to be named. The parentheses
indicate that this is a method.
e. String[] args is a single parameter for the method. String[] is the type of the parameter,
indicating an array of Strings. args is the name of the parameter. Parameters must be
named. args can be any name.
10. Use of Scanner: It is used to take input from keyboard in output screen.
Example for string:
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hey, Whats your name?");
String name = input.next();
System.out.println("Hello " +name);
}//main
}//class
Example for number:
import java.util.Scanner;
public class InputInt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your favorite number?");
int number = input.nextInt();
System.out.println("You entered: " + number);
}
}
11. Program to add two numbers:
import java.util.Scanner;
public class HelloWorld {
public static void main(String[]args)
{
//int a;
int b;
double sum;
Scanner input= new Scanner(System.in);
System.out.println("enter the value of a");
int a = input.nextInt(); // here variable a is not declared
therefore declaring with int a.
System.out.println("enter the value of b");
b = input.nextInt();
sum= a+b;
System.out.println("sum of numbers is: " + sum);
input.close();
}
}
12. Java Enums:
Enums were introduced in java 5.0. Enums restrict a variable to have one of only a few
predefined values. The values in this enumerated list are called enums.
With the use of enums it is possible to reduce the number of bugs in your code.
For example, if we consider an application for a fresh juice shop, it would be possible to
restrict the glass size to small, medium and large. This would make sure that it would not
allow anyone to order any size other than the small, medium or large.
Example:
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
}
}
Above example will produce the following result:
Size: MEDIUM
Note: enums can be declared as their own or inside a class. Methods, variables, constructors
can be defined inside enums as well.