Join Regular Classroom : Visit ClassroomTech

Java Coding For Interview | Codewindow.in

How to Print a Welcome message in JAVA? What is the difference between print, printf and println?

package CodeWindow;
public class Introduction {

	public static void main(String[] args) {
		System.out.println("Welcome to CodeWindow");
		/*
		 * System.out.println()
		 * It will print the content and will also add
		 * a new line at the end. 
		 */
		System.out.print("Welcome to CodeWindow");
		/*
		 * System.out.print()
		 * It will print the content and will not add
		 * a new line at the end. 
		 */
		System.out.printf("\n%d", 10);
		System.out.print("\n"+10);
		
		System.out.printf("\n%s", "CodeWindow");
		System.out.printf("\n%f", 3.14);
		/*
		In case of printf() we have to mention format of the data type.
		*/
	}

}

Welcome to CodeWindow
Welcome to CodeWindow
10
10
CodeWindow
3.140000

public class Introduction
1. public-If we define a class as public then we need to create a file as Class_name.java. So, Here File name will be Introduction.java
2. class– Keyword.
3. Introduction – ClassName

public static void main(String[] args)
public – Every main method must be public.
static – main() method must be static as we need not create object of the class. static method can be accessed without creating any objects of the class.

If we do not mention static, It will generate error.
void – return type of main()
main()– main() method
String [] args / String args[] – It is used for command line argument.
It is mandatory in the program.

System.out.println();
System – It is final class that is within java.lang.package
out – This is an instance of PrintStream type which is a public ad static member field of the System class.
println()– It is a method of PrintStream Class.

 

Take user input in Java in any way you know.

package CodeWindow;

import java.util.Scanner;

public class ValueInput {

	public static void main(String[] args) {
		Scanner input= new Scanner(System.in);
		int i=input.nextInt();        
		float f=input.nextFloat();
		double d = input.nextDouble();
		String s = input.next(); 
		input.nextLine();
		String t = input.nextLine();

		System.out.println(i);
		System.out.println(f);
		System.out.println(d);
		System.out.println(s);
		System.out.println(t);
		
		input.close();
		// To Avoid Resource leak
	}

}

Input:
2021
3.14
2.0
CodeWindow
Classroom

Output:
2021
3.14
2.0
CodeWindow
Classroom

Command Line Argument in JAVA.

package CodeWindow;
public class CommandLineArgument {

	public static void main(String[] args) {
		for(int i=0;i<args.length;i++) {
			System.out.print(args[i]+" ");
		}
	}
}

Input:
CodeWindow 2021 2.0

Output:
CodeWindow 2021 2.0

How to execute in Eclipse:
Run -> Run Configurations -> Arguments -> Program Arguments -> Set Arguments -> Run

Constructor is a special method that is used to allocate memory for every object creation.
There are three rules defined for the constructor.
1. Constructor name must be the same as its class name
2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

There are two types of constructors in Java:
1. Default constructor (No argument constructor)
2. Parameterized constructor

Write a code to implement Default Constructor in JAVA

package CodeWindow;
class Student1 {
	private int roll;
	private String name;
	
	public void display() {
		System.out.println(this.roll);
		System.out.println(this.name);
		/*
		 * 'this' is used to define Calling Object. 
		 */
	}
	
	public Student1(){
		this.roll=0;
		this.name="Anonymous";
	}
}
public class Default_Constructor {

	public static void main(String[] args) {
		Student1 Riddhi = new Student1();
		Riddhi.display();
	}

}

Output:
0
Anonymous

Write a code to implement Parameterized Constructor in JAVA

package CodeWindow;
class Employee {
	private int id;
	private String name;
	public Employee(int id, String name){
		this.id=id;
		this.name=name;
	}
	public void display() {
		System.out.println(this.id);
		System.out.println(this.name);
		/*
		 * 'this' is used to define Calling Object. 
		 */
	}
}
public class Parameterized_Constructor {

	public static void main(String[] args) {
		Employee Deb = new Employee(1021,"Debapriya");
		Deb.display();
	}
}

Output:
1021
Debapriya

Write a code to implement Method Overloading.

Method Overload – It is Compile Time Polymorphism.
Poly means Many
Morph means Form.

Here A class( OverloadDemo) can contain more than one method with the same method name but parameter list must be different.
This concept is known as method overload.

package CodeWindow;
public class OverloadDemo {
	private int x;
	private int y;
	private double m;
	private double n;
	private float f;
	private float g;
	void display() {
		System.out.println("A method with 0 Parameters");
	}
	void display(int a) {
		this.x=a;
		System.out.println("A method with 1 Parameters and value is: "+ this.x);
	}
	void display(int c,int d) {
		this.x=c;
		this.y=d;
		System.out.println("A method with 2  Parameters and values are: "+this.x + " " + this.y);
	}
	void display(double c,double d) {
		this.m=c;
		this.n=d;
		System.out.println("A method with 2  Parameters(Double) and values are: "+this.m + " " + this.n);
	}
	void display(float f,float g) {
		this.f=f;
		this.g=g;
		System.out.println("A method with 2  Parameters(float) and values are: "+this.f + " " + this.g);
	}
	void display(int c,double d) {
		this.x=c;
		this.n=d;
		System.out.println("A method with 2  Parameters and values are: "+this.x + " " + this.n);
	}
	void display(double d,int c) {
		this.n=d;
		this.x=c;
		System.out.println("A method with 2  Parameters and values are: "+this.n + " " + this.x);
	}
	public static void main(String[] args) {
		OverloadDemo obj1=new OverloadDemo();
		obj1.display();
		obj1.display(10);
		obj1.display(10, 20);
		obj1.display(50.85, 20.75);
		obj1.display(40.85f, 80.75f);
		obj1.display(85, 20.75);
		obj1.display(20.85,74);
		
	}
}

Method Overloading
1. One class Same Method more than one.
2. Parameter list should be differed.
Like:
void display();
void display(int a);
void display(int a, int b);
void display(double x, double y);
void display(int a, double d);
void display(double d, int a);
3. Method overloading doesn’t depend on return type of a method.
4. Different methods may have same number of parameters but data type of parameters should be different.
A) No of parameters different.
B) No of parameters similar–
i) Data types of parameters
ii) Order of parameters.


It depends upon No of Parameters and Order of Parameters.

Write a code to implement Constructor Overloading.

package CodeWindow;
class Teacher{
	private int tid;
	private String name;
	
	//Default Constructor
	public Teacher(){
		this.tid=12;
		this.name="Anonymous";
	}

	//Parameterized Constructor
	public Teacher(int tid, String name){
		this.tid=tid;
		this.name=name;
	}
	
	public Teacher( String name, int tid){
		this.tid=tid;
		this.name=name;
	}
	
	public void display() {
		System.out.println(this.tid);
		System.out.println(this.name);
		/*
		 * 'this' is used to define Calling Object. 
		 */
	}
}

public class Constructor_Overloading {
	public static void main(String[] args) {
		Teacher t1=new Teacher();
		t1.display();
		Teacher t2=new Teacher(14,"James");
		t2.display();
		Teacher t3=new Teacher("Joseph",45);
		t3.display();
	}

}
Recent Posts
Pages