What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called Greentalk and the file extension was .gt . After that, it was called Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...
Literals
A constant value that can be assigned to the variable is called a literal.
- 7 : Integer literal
- 7.5f : Float literal
- 10.2 : Double literal
- 'D' : Character literal
- true : Boolean literal
- "Deepak" : String literal
class Literals {
public static void main(String[] arr) {
int i1 = 10;
int i2 = 20;
int sumInt = i1 + i2;
System.out.println("The sum of two numbers is: " + sumInt);
float f1 = 10;
float f2 = 20;
float sumFloat = f1 + f2;
System.out.println("The sum of two numbers is: " + sumFloat);
double d1 = 10;
double d2 = 20;
double sumDouble = d1 + d2;
System.out.println("The sum of two numbers is: " + sumDouble);
char c = 'D';
System.out.println("The character is: " + c);
boolean b = true;
System.out.println("The Boolean is: " + b);
String s = "Deepak";
System.out.print("The string is: " + s);
}
}
Output :
The sum of two numbers is: 30
The sum of two numbers is: 30.0
The sum of two numbers is: 30.0
The character is: D
The Boolean is: true
The string is: Deepak
Keywords
A keyword is a reserved word and used by the Java compiler. We cannot use it as a variable name, constant name etc.
Taking User Input
- Scanner class of java.util package is used to take input from the user's keyboard.
- The Scanner class has many methods for taking input from the user depending upon the type of input.
- To use any of the methods of the Scanner class, first, we need to create an object of the Scanner class.
import java.util.Scanner;
class UserInput {
public static void main(String arr[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter integer value 1: ");
int x = input.nextInt();
System.out.print("Enter integer value 2: ");
int y = input.nextInt();
System.out.println("The sum of two numbers is " + (x + y));
System.out.print("Enter float value 1: ");
float a = input.nextFloat();
System.out.print("Enter float value 2: ");
float b = input.nextFloat();
System.out.println("The sum of two numbers is " + (a + b));
input.nextLine();
System.out.print("Enter a string: ");
String str = input.nextLine();
System.out.println("You entered: " + str);
System.out.print("Enter a character: ");
char cha = input.next().charAt(0);
System.out.println("You entered: " + cha);
input.close();
}
}
Output :
Enter integer value 1: 10
Enter integer value 2: 20
The sum of two numbers is 30
Enter float value 1: 10
Enter float value 2: 20
The sum of two numbers is 30.0
Enter a string: Deepak
You entered: Deepak
Enter a character: D
You entered: D
Enter integer value 2: 20
The sum of two numbers is 30
Enter float value 1: 10
Enter float value 2: 20
The sum of two numbers is 30.0
Enter a string: Deepak
You entered: Deepak
Enter a character: D
You entered: D
Note: The close() method closes the Scanner which has been opened. The close() method does not return any value. This is prevent from the memory leakage problem.
Comments
The Java comments are the statements in a program that are not executed by the compiler and interpreter.
- Comments are used to make the program more readable by adding details of the code.
- The comments can be used to provide information or explanation about the variable, method, class or any statement.
- It can also be used to prevent the execution of program code while testing the alternative code.
There are three types of comments in Java:
- Single Line Comment
- Multi Line Comment
- Documentation Comment
Single Line Comment
- The single-line comment is used to comment only one line of the code.
- Single line comments starts with two forward slashes (//).
class SingleLine { public static void main(String[] arr) { int d = 7; // d is the variable name with value 7 System.out.print(d); // Printing the value of variable d }}
class SingleLine {
public static void main(String[] arr) {
int d = 7; // d is the variable name with value 7
System.out.print(d); // Printing the value of variable d
}
}
Output :
7
Multi Line Comments
- The multi-line comment is used to comment multiple lines of code.
- It can be used to explain a complex code snippet or to comment multiple lines of code at a time.
- Multi-line comments are placed between /* and */.
class MultiLine { public static void main(String[] arr) { /* Let's declare, initialize and print the value of variable. */ int d = 7; System.out.print(d); }}
class MultiLine {
public static void main(String[] arr) {
/*
Let's declare,
initialize and
print the
value of variable.
*/
int d = 7;
System.out.print(d);
}
}
Output :
7
Documentation Comment
- Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API(Application Programming Interface).
- These API's are needed for reference, i.e., which classes, methods, arguments etc. are used in the code.
- The documentation comments are placed between /** and */.
class Documentation { public static void main(String[] arr) { /* * Let's declare, initialize and print the value of variable. * Here we have d as our variable name declared in the main method inside Documentation class. * Then we print the value of variable d. */ int d = 7; System.out.print(d); }}
class Documentation {
public static void main(String[] arr) {
/*
* Let's declare, initialize and print the value of variable.
* Here we have d as our variable name declared in the main method inside Documentation class.
* Then we print the value of variable d.
*/
int d = 7;
System.out.print(d);
}
}
Output :
7
Comments
Post a Comment