Tutorials Hut

Tutorials Hut

Java Basic : Syntax, Class, Object and More

Let’s begin with the basics of java programming to understand more about it, as you must have already read in previous chapters that java is an object-oriented programming language.

In java programs we first design a class and then create an object of class and then access methods or variables of class based on our logic or requirement.

class object methods

It is important to understand class, object, methods and variables to start programming.

1) Class – Class is a skeleton of real-world objects for example a Tree class in java will act as an escalation in our program to represent a tree and by using this skeleton we can create as many as tree objects we want.

2) Object – Object is an instance of class for example if we create a Tree class and then we create tree1 instance of that class then this tree1 will be called an object of Tree class.

3) Methods – A method or function in java class represents a behaviour of that class or object for example in a Tree class giveOxyzen(), absorbCarbondioxide () etc. will be called methods.

4) Variables – A variable is an attribute of class which represents some property of class, for example numberOfBranches or numberOfLeaves will be called variables of a class.

First Simple Java program

Follow below steps to write a simple java program, you should have jdk and all setup ready in the system, if not you can read our article on JDK for complete understanding and installation instructions.

1) You can open a notepad (windows) or Subline text (MAC) and write below code and save as Tree.java

Code:

public class Tree {
public static void
main(String[] args) {
System.out.println("Hello, I am a tree");
}}

2) Now open iTerm/console (MAC) or command prompt (CMD) on Windows, go to the folder where you saved your Tree.java file and run below command.

Command:

javac Tree.java

This will compile java file and generate bytecode class Tree.class

3) Now run below command, this will run generated bytecode class Tree.class and show output on console.

 

Command:

Java Tree

 

Output on console:

Hello, I am a tree

 

Wow! You have completed your first sample java program; next you can read our JRE and JVM related chapters which will make a base for your java study.

 

Basic Syntax

Please note below points while writing a java program:

1) Case sensitivity Java is a case sensitive programming language that means any reserved keywords or variable or method or class name case can’t be changed from lower to upper or upper to lower case, for example – if have declared a class with name “Tree”, you can’t refer it with “tree” or “tRee” this may give error.

2) Class names Class name’s first letter should be capital, if class name has many words each word’s first letter should be in capital letter; you may choose to differ but it is wrong practice.

Few examples of good class names –
Tree
Planet
School
Account
SavingAccount
FixedDepositAccount

Few examples of bad class names –

tree
planet
SchooL
savingAccount
Fixeddepositaccount

3) Method and Variable Names – Method or function name in java should always start with small letters and if it contains more than one word then the first letter of each word should be capital letter.

Few examples of good method names in a Tree class-
giveOxyzen()
absorbCarbondioxide()
doPhotoSynthesis()

Few examples of bad method names in a Tree class-
GiveOxyzen()
Absorbcarbondioxide()

Variables names should also be similar to method names.

4) Java program file name – File name should be the same as class name and should follow the same naming conventions as class name, you may choose to differ but is wrong and will have issues in future programming.

note

Few examples of good file names –
Tree.java
Planet.java
School.java
Account.java
SavingAccount.java
FixedDepositAccount.java

Few examples of bad file names –
Tree.java
Planet.java
SchooL.java
savingAccount.java
Fixeddepositaccount.java

5) main() method – In java if you want to start a program the class loaders will look for main method first and then start execution from there, so main method is the entry point in standard java edition J2SE.

See below syntax of main method in any java class:

public static void main(String args[]) { }

Java Identifiers

All java class, variable and method names are called “identifiers”, we already saw some good examples of these in previous paragraphs.

Java Modifiers

There are two categories of modifiers:

1) Access modifiers – default, public, protected and private.

2) Non access modifiers – final, abstract etc.

Java Variables

Below are types of variables in java program:

Java Variables

Comments in Java Program

You may choose to add comments in a java program for providing some details about a class or a method but always remember that comments are never executed in java.

Adding comments is a very good practice in writing a real world java program.

There are two ways to write comments in any java program, let’s have a look-

1) By using two forward slash (// comment) – This can be used if you want to add single line comments.

2) By using forward slash with star (/* comment */) – This can be used if you want to write multiline comments in your java program.

See below example of both –
Just below class Tree there is multiline comment and then after the main method there is a single line comment.

public class Tree {
   /* This is my first java program the name of class is Tree.
	* This is an example of multiline comment
	*/
	public static void main(String[] args) {
   	// This is an example of single line comment
    	System.out.println("Hello, I am a tree");
	}
}

Java Enums

Enum is a keyword in java which is used to create a specific set or group of values which can’t be changed and will serve as a type of things. Enums are used for creating specific type of variables but remember they are not variables.

Enums were introduced in java 5.0 and are very popular in defining typed variables, for example if you want to define RED, BLUE and GREEN as standard color to be used in a program and no any other thing should be used, so you can define them in Enum and use them everywhere.

Another example of enum can be size, if you want to define SMALL, MEDIUM and LARGE as only size in your code then you can create enum of them.

Example:

class Color{
enum ColorNames{ RED, BLUE, GREEN}
ColorNames name;
}

public class ColorTest {

public static void main(String args[]) {
Color color= new Color();
color.name = Color.ColorNames.RED ;
System.out.println("Color: " + color.name);
}
}
Output: Color: RED

Java Keywords

Below are some reserved keywords in java:

abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

enum

extends

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

try

void

volatile

while

   

Next Articles To Read

 


















  • Scroll to Top