Click here to Skip to main content
16,016,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.io.*;
public class Employee {

   public String name;

   private double salary;

   public Employee (String empName) {
      name = empName;
   }
   public Employee (double empSal) {
      salary = empSal;
   }

   public void printEmp() {
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);
   }

   public static void main(String args[]) {
     Employee empOne = new Employee("Ransika");
     Employee emptwo = new Employee(1000);
      empOne.printEmp();
      emptwo.printEmp();
   }
}


What I have tried:

output :
$javac Employee.java
$java -Xmx128M -Xms16M Employee
name  : Ransika
salary :0.0
name  : null
salary :1000.0
Posted
Updated 10-Apr-18 1:25am

1 solution

Variables are initialised with a default value upon creation. For numeric types like boolean, integer, and floating point this is the value zero and for object types like string it is null. If you do not set a variable by assignments in your code they will contain their default value.

See also Default Values at Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)[^].
 
Share this answer
 
Comments
CPallini 10-Apr-18 7:33am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900