Java Coding

Java Coding

Java coding is simple to practice. First reason, I give is Java does not support pointers. Due to many predefined methods available, with less code and in less time, you can do your task. For this reason, Java is known as "Production Language"


I give simple Java coding example printing "Employee Salary" for a very Fresher. For simplicity, I use one instance variable (C-lang global variables are known as instance variables in Java coding), one local variable and one method.

Java coding
Output screenshot of Java coding example of

Let us discuss briefly about the code line-by-line.

import java.lang.*;

To understand fast, I compare the Java coding with C-lang coding. import is a keyword of Java equivalent to include of C-lang. java.lang is known as package. A package equivalent to a header file. Here, we are importing java.lang package into our Java coding. The java.lang package contains classes like String and System etc. String and System classes are used in the code, as you observe.

public class Employee

All the code you write should be enclosed in class declaration, within open and close braces. Here, the class name is Employee. public means the Employee can be accessed by any other class without any restrictions.

int basicPay = 5000;

basicPay is instance variable. The instance variable can be called from any method of the class.

public void calculate()

calculate() is a method. A method is equivalent to a function of C-lang.

double allowances = 2000.59;

allowances is a local variable. As a local variable, allowances is accessible from calculate() method only.

System.out.println(“Employee total salary Rs.” + (basicPay + allowances));

System.put.println() is equivalent to printf() of C-lang which prints messages at command prompt. Observe, Java coding does not support place holders like %f or %d. Just put all variables within parentheses concatenating with string Employee total salary Rs. within double quotes. Just place + symbol between string and variables for concatenation.

public static void main(String args[])

It is the Java coding syntax of main() method. it is a lengthy one compared to main() function of C-lang. Here, public means any class can access main(), static means the main() method does not require an object to call, void means main() does not return a value and finally the parameter of main() is a String array by name args. args is useful to access command-line arguments.

Employee emp1 = new Employee();
System.out.println(“Thank You”);

Java is an object-oriented language and everywhere objects are used in Java coding. emp1 is an object of Employee. Observe the syntax of creating a Java object. Write class name on both sides of =. With object emp1 call calculate() method. With another System.out.println() statement, print "Thank You" message. Observe one more feature of Java coding, we have not used \n escape sequence. \n is inbuilt. In println(), ln stands for new line character \n. Here, brief discussion of code is given for for more clarification and explanation, go though the following links.

Following links of this same Web site gives you more explanation on starting topics to learn Java coding like syntax of creating objects, difference of calling local and instance variables, static and public keywords etc. Slowly go in the same order.

1. Java Example Compilation Execution.
2. Java Local Instance Variables.
3. Data Binding Data Hiding Encapsulation.

Leave a Reply

Your email address will not be published. Required fields are marked *