Basics
Java is one of the most popular programming languages in the world. With Java you can build various types of applications such as desktop, web, mobile apps and distributed systems.
5 Interesting Facts about Java
1. Java was developed by James Gosling in 1995 at Sun Microsystems (later
acquired by Oracle).
2. It was initially called Oak. Later it was renamed to Green and was finally
renamed to Java inspired by Java coffee.
3. Java has close to 9 million developers worldwide.
4. About 3 billion mobile phones run Java, as well as 125 million TV sets and
every Blu-Ray player.
5. According to indeed.com, the average salary of a Java developer is just over
$100,000 per year in the US.
Boilerplate
public class A1 {
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Showing Output
System.out.println("Hello World");
Taking Input
import java.util.Scanner; //import scanner class
public class A1 {
public static void main(String args[])
{
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// take input from the user
String varName = input.nextLine();
}
}
Primitive Type Variables
The eight primitives defined in Java are int, byte, short, long, float, double, boolean, and char those aren't considered objects and represent raw values.
Declaring Variables
byte a1 = 18;
int a2 = 256;
long a3 = 3_123_456L;
float a4 = 10.99F;
short a5 = 786;
char a6 = 'A';
boolean isEligible = true;
Comments
A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.
Single-Line Comment
//AJ Blogs
Multi-Line Comment
/*AJ Blogs
Multi-Line Comment */
Arithmetic Operations
Addition
int x = 100 + 3;
Subtraction
int x = 100 - 3;
Multiplication
int x = 100 * 3;
Division
int x = 100 / 3;
float x = (float)10 / (float)3;
Modulo Remainder
int x = 100 % 3;
Strings
It is a collection of characters surrounded by double quotes.
String Methods
//Strings
//Declaring String
String s= "Hello my name is AJ Blogs";
//length() for finding length of a string
System.out.println(s.length());
//method to convert every single letter into lower case.
System.out.println(s.toLowerCase());
//method to convert every single letter into upper case.
System.out.println(s.toUpperCase());
//Trim Method
String r= " Hello my name is AJ Blogs ";
System.out.println(r);
System.out.println(r.trim());
//Returns the index of specified character from the string
String var_name = "AJ Blogs";
System.out.println(var_name.indexOf("o"));
//Used to concatenate two strings
String var1 = "AJ";
String var2 = "Blogs";
System.out.println(var1.concat(var2));
//Substring Method
System.out.println(s.substring(3));
System.out.println(s.substring(3,15));
//Replace
System.out.println(s.replace("e","r"));
String letter = "Dear <|name|>, Thanks a lot!";
letter = letter.replace("<|name|>", "AJ Blogs");
System.out.println(letter);