Java programming basics for beginners5 min read

Java Programming Basics

When writing a program in Java programming language, below are some important points that you must follow –

  • Case Sensitivity – Java is case sensitive, which means that identifiers are case sensitive. In Java, Hey and hey would have different meanings.
  • Class Names – The first letter of each class name should be capitalized. If a class name is made up of numerous words, the first letter of each inner word should be in capital letters.
    • Example: class MyFirstClass
  • Method Names – All method names must begin with a lowercase letter. If several words are used to form the method’s name, the first letter of each inner word should be in Upper Case.
    • Example: public void myMethodName()
  • Program File Name – The program file name should be identical to the class name.
  • When saving the file, use the class name and append .java’ to the end of the name (remember, Java is case sensitive) (if the file name and the class name do not match your program will not compile).
    • Example: Assume ‘MyFirstJavaProgram’ is the class name.
      Then the file should be saved as ‘MyFirstJavaProgram.java
  • public static void main(String args[ ]) – Java program processing starts from the main() method which is a mandatory part of every Java program.
Tutorial on basics of Java programming for beginners

Classes and objects

Class

A class can be defined as a blueprint or a prototype that outlines the actions and states that an object of its type can support. In other words, we can say that a class is a template from which individual objects are built-in Java language. The class body follows the class declaration and is embedded within the curly braces. The class body contains the declaration for all instance variables and class variables known collectively as member variables for the class. In addition, the class body contains declarations and implementations for all the instance methods and class methods for the class.

A sample of a class is given below:

public class Dog{
	String breed;
	int age;
	String color;
	void barking(){
	}
	void hungry(){
	}
	void sleeping(){
	}
}

Objects in Java

In Java, you can create an object by creating an instance of a class.

  • Let’s take a closer look at what objects are. Many objects, such as cars, dogs, and humans, can be found in the actual world. All of these items have a state and behave in a certain way.
  • If we consider a dog, its state includes its name, breed, and color, as well as its behavior, which includes barking, wagging, and running.
  • When you compare a software object to a real-world thing, you’ll see that they have a lot in common.
  • Software objects contain states and behaviors as well. The state of a software object is recorded in fields, and its behavior is displayed via methods.
  • Methods in software development work on an object’s internal state and object-to-object communication are accomplished by methods.

Creating an Object

When creating an object from a class, there are three steps :

  • Variable declaration: A variable declaration that includes a variable name and an object type.
  • Instantiation: The ‘new’ keyword is used to create the object during instantiation.
  • Initialization: The ‘new’ keyword is followed by a function call. The new object is initialized with this call.

After the completion of creating an object you probably want to use it for something. You may need some information from it, want to change its state, or have it perform some action. Objects give you two ways to do these things :

  1. Manipulate or inspect its variables.
  2. Call its methods.

Generally, object-oriented programming does not promote the direct manipulation of an object’s variables, as this can lead you further to potentially put the object in an inconsistent state. An ideal object, on the other hand, provides methods for inspecting and changing its state. These methods ensure that the object is never in a state of inconsistency. In other cases, though, it is more practical to use an object’s variables directly.


Constructors

Constructors are an important sub-topic to explore when discussing classes. There is a function Object() in every class. If we don’t write a function Object() for a class directly, the java compiler creates a default function Object() for that class. At least one function Object() will be called whenever a new object is generated. Constructors should have the same name as the class as a general rule. There can be multiple constructors in a class.

An example of a constructor is given below :

public class Puppy{
	public Puppy(){
	}
	public Puppy(String name){
	// This constructor has one parameter, name.
	}
}

Creating a Java Application

Creating a Java application is rather easy, once you know how it works.

The basic layout of a Java program is similar to that of a C# program. The Hello World program in Java looks like below

public class Hello{
	public static void main(String args[]){ 
		System.out.println("Hello World"); 
	}
}

This sample program is not really difficult to understand. It defines a class called Hello World and has a method called main which most people may know from C or C++ programming. Within the main function, we use the println() function to write the message “Hello World” on the screen. The println() function is similar to printf in C.

Note: When compiling Java applications make sure that your file has the name as your main class. If it does not, the java compiler will throw an error.


Share:

Leave a Reply