Core Java
• Create a Java Project
Step 1 – Open Eclipse
Step 2 – File->New->Java Project
Step 3 – Right click on “src” folder and select package and enter any package name
Step 4 – Right Click on package and select Class
Step 5 – Enter any Class Name and the click Finish(no space should be present in Class name)
• Create a main method which is the entry point of any application
public static void main(String[] args)
Syso(“in main method”);
• Calling a method of Class
Create a object of the class whose method has to be called
FirstClass.java
public class FirstClass
{
public void Method1()
{
System.out.println("Method 1");
}
Main Class.java
FirstClass obj=new FirstClass();
obj.Method1();
• Parameterized method
public void Method1(int a,int b,String c)
{
Syso(a+b);
Calling parameterized method
Obj.Method1(10,20,”Hello”);
• Method return type
public int Method1(int a,int b)
Int c=a+b;
return c;
Calling
Int d=obj.Method1(10,20);
System.out.println(d);
• Static Method
FirstClass.java file
public static void StaticMethod()
Syso(“In static method”);
Calling
To call a static method we don’t have to create a object it can be called directly from class name
FirstClass.StaticMethod();
• Data Conversion
To convert into different data types we can use wrapper class
Int to String
Int i=10;
String.valueOf(i)
String to Int/double/float etc
String str=”10”;
Integer.vaueOf(str);
Double.vaueOf(str);
Float.valueOf(str);
• Type Casting
Implicit type casting – Compiler automatically converts
Used if we assign smaller data type to bigger data type
Int a=10;
float b=a;
Here float is the bigger data type so complier will automatically converts i.e implicit data type
Explicit type casting – We need to specify the conversion
Used if we assign bigger data type to smaller one
double d=24.565656565656;
int a=(int)d;
Note: We can cast only same group of data i.e double to int meaning only number not string to number or
vice versa.
• Difference between String and StringBuilder
The original value of string does not change but the original value of StringBuilder changes.
String str=”This is demo”;
StringBuilder strbui=new StringBuilder(“this is string builder”);
Adding string welcome in both
str.concat(“Welcome”);
strbui.Append(“Welcome”);
System.out.println(str); - The output will be original string i.e ”This is demo”;
System.out.println(strbui); - The output will be appended string i.e ” this is string builderWelcome”;
• Constructor
If we create a method with the same name as Classname then it’s a constructor, the only difference
constructor does not have a return type.
Default Constructor
public class FirstClass
{
public FirstClass()
{
System.out.println("Constructor");
}
Calling of Constructor – When we create an object constructor is automatically called
FirstClass obj=new FirstClass();
Parameterized Constructor
public class FirstClass
{
public FirstClass(int a,int b)
{
System.out.println(a);
System.out.println(b);
}
FirstClass obj=new FirstClass(10,10);
This keyword
If we have to refer the class level variable we can use this keyword
public class FirstClass
{
Int a=10;
String b=”Hello”;
public void Method(int a,int b)
{
System.out.println(this.a);
System.out.println(this.b);
}
Output:
10
Hello
• Loops
For loop
String[] arr=new String[3]
arr[0]=”hi”;
arr[0]=”hello”;
arr[0]=”java”;
for(int i=0;i<arr.length;i++)
Syso(arr[i])
While loop
Int j=0;
while(j<arr.length)
Syso(arr[i]);
J++;
For-Each loop
for(String s:arr)
{
Syso(s);
• Collections
ArrayList
ArrayList<String> arrlist=new ArrayList<String>();
arrlist.Add(“Hi”);
arrlist.Add(“Bye”);
arrlist.Add(“demo”);
Iterate in array list using for each loop
for(String item:arrlist)
Syso(item);
HashTable
Hashtable<String, String> hst1 = new Hashtable<String, String>();
hst1.put("x", "a");
hst1.put("y", "b");
Iterate in HashTable
for(Map.Entry<String,String> hs:hst1.entrySet())
{
System.out.println(hs.getKey());
System.out.println(hs.getValue());
}
HashMap is same as HashTable only in HashMap you can store null values