Java Programming Language

Go back to tutorial

Learning Java Programming Language

In order to learn Java Programming Language it is very important for you to have a good hands on learning is very important. So to begin with let us understand the functionalities of Java Programming Language. As we may define, Java is truly a object-oriented programming language.  so any Java program will contain at least one class, and any Java program may be viewed as a collection of classes.

Classifying Java Programs 

  • Console application programs with text-based user interfaces
  • GUI-based standalone application programs
  • GUI-based applets that can be downloaded over the Internet and run on a local machine inside a web browser
  • Server-based programs, called servlets, that can be invoked by a server to produce some information that the server can then send to a client over the web.

Types of Java Program Entities

Beginning from “smallest” to “largest”, the entities that comprise a Java program include –

  • Characters
  • Tokens
  • Expressions and statements
  • Methods
  • Classes
  • Packages
  • Program itself

Types of Comments

Correspondingly, Java provides three kinds of comments, where two are directed at human readers. But third comment, although human readable, is directed more at the javadoc utility. In which case it reads these comments and produces HTML files that are documented in a standard way the code where these special comments are found.

  • Single-line or end-of-line comments introduced by // and terminated by the end of the line, as in // We can combine other operators with the assignment operator.
  • Multi-line comments or within-code comments are enclosed by /* and */
  • Special javadoc-directed comments are enclosed by /** and */, as in

Identifiers and Reserved Words

we may define identifier as the name of a programmer-defined entity. In Java programming Language, any identifier must begin with a letter, followed by any number of letters and/or digits. In this context, the underscore (_) and the dollar sign ($) are considered to be letters, though their use in identifier names is discouraged.

Java Programming Language has certain defined identifiers, which cannot be used other than the way they are intended to be used by the Java designers. Also referred as reserved words, or just keywords.

Type Compatibility and Type Conversions

It is extremely important to know when you can use a value of one type, even though a value of a different type is formally called for. And also to understand when can you safely convert a value from one type to another. This is to say, it is crucial to understand the notions of type compatibility and type conversion.

The term type conversion refers to the conversion of a value of one type to a value of another type. Generally conversion between numerical types is permitted, as well as between values of type char and integer values within a certain range. Further, a conversion from a “narrower” (smaller) type to a “wider” (larger) type will take place automatically without complaint from the compiler, while a conversion from a wider to a narrower type must be explicitly cast.

Note, a type T1 is compatible with a type T2 if a value of type T1 can appear wherever a value of type T2 is expected, and vice versa. Also, the Conversion between object (reference) types is also possible, but trickier.

Expressions and Operators

Not to mention, the basic Java operators are similar to those of C++, such that similar expressions behave similarly. We will briefly discuss about the various Java operators briefly,

  • Since the char type is considered a numerical data type in Java, all operations that can be applied to values of integer types can be applied to values of type char.
  • The == and != operators can be applied to any type, though they may not return what you expect. (Example – String data type is a prime example)
  • The operators <, <=, > and >= can only be applied to numerical types.
  • The % operator can be applied to floating point types as well as integer types. The meaning of this operator is given by x % y == x – (x/y * y)
  • Java integer arithmetic never overflows or underflows. Instead, it “wraps modulo the range”.
  • Java floating-point arithmetic never throws an exception under any circumstances, even division by 0. Instead, there are two “magic numbers” to take care of “unusual results”: “infinity” and “NaN” (Not a Number).
  • Since the && and || operators perform short-circuit evaluation but & and | do not. Therefore, you should use & and | any time you want to combine boolean expressions. This is done to prevent short-circuit evaluation from being invoked on the resulting combined expression.

Types of Statement 

We have shared below a short summary list of the available statements in Java Programming Language –

  • expression statements (including assignment statements)
  • compound statements (delimited, as usual, by braces)
  • empty statements
  • labeled statements (used as the target of a break, for example)
  • variable and object declaration/initialization statements
  • selection statements, which look and behave like those in C++ – if, if..else, switch
  • looping statements, which look and behave like those in C++ – while, do..while, for
  • unconditional jump statements (break and continue may have a label in Java)
  • break must be enclosed in a loop or switch statement; it terminates the immediately enclosing loop or switch statement.
  • break label must occur within a labeled compound statement (loop statement, switch statement, or statement block); it transfers control to the statement immediately following the labeled compound statement.
  • continue must be enclosed in a loop; it terminates the current iteration of the immediately enclosing loop and starts the next iteration of that loop.
  • continue label must occur within a labeled loop statement; it terminates the current iteration of the labeled loop statement and starts the next iteration of that labeled loop statement.
  • return

Assignment Behavior

  • Assignment of primitive types behaves as expected.
  • On the other hand, assignment of objects always assigns only the reference or “handle” to the object, not the object itself.

Java Programming Language Methods 

Java method are quite similar to a C++ function. But with following pointers to note,

  • Java method cannot exist outside a Java class. This is to say, the C++ “global function” has no analogue in Java.
  • Parameters are always and only pass-by-value in Java, whether they are primitive values or objects. There is no equivalent to the C++ “reference parameter”.
  • Since exception handling is an integral part of the Java landscape than the C++ landscape. Therefore, you will more frequently see methods that throw an exception.

Note, Java methods can be overloaded. This is to say, a class can have several methods with the same name, as long as the parameter lists are different.

Java Programming Language – Classes and Objects

Java class definition looks quite similar to a C++ class definition. But with the following pointers,

  • No semi-colon after the last curly bracket.
  • Each member declaration needs its own access control qualifier (public, private, or protected).
  • Maximum one public class is permitted in any file, and the name of the public class (if any) in a file must be the same as the file name.

Class in Java may have four different kinds of members:

  • class fields (are static fields)
  • class methods (are static methods)
  • instance fields
  • instance methods

Creating and initializing objects in Java

For creating and initializing objects, classes need constructors, following points must be considered,

  • Java class constructor has the same name as its class and no return type.
  • In Java, there is a this keyword, which refers to the current object, and it is the job of any constructor to initialize the object referenced by this.
  • Each class should have a default constructor definition, though Java will provide one anyway, provided there are no other constructors.
  • Also a class may have multiple constructors, provided they are distinguished by different parameter lists.
  • One constructor may be invoked from a second, provided a call of the form this(parameter_list_of_first_constructor);    appears as the first statement in the body of the second constructor.
  • Class data members may be initialized directly, or by a constructor. If this is not done, each will have a default value determined by its data type. The default value for any object type (reference type) is null, which is a Java keyword.
  • An initializer block can also be used for initializer purposes.

Access Modifiers and Rules

Members (fields and methods) of a class may be given different levels of access from other parts of a program. This helps to provide “data hiding” and supports encapsulation. Let us suppose a class is in a given package. Then, ranging from most to least accessible, the options for access to a method or variable in that class include –

  • public: accessible from anywhere
  • protected: accessible from any class within the given package, and also from subclasses of the given package that appear in other packages
  • default (or “friendly”): accessible from any class in the given package (There is no keyword for this type of access.)
  • private: accessible only from within the class itself

Subclasses and Inheritance

Java permits single inheritance. That is, a class may inherit from one (and only one) base class. The syntax is given below,

class Base


// Definition of class Base


class Derived extends Base


// Definition of class Derived

// All members of Base automatically included


Important Notes

  • “Mother of All Classes” is class Object, from which any class automatically (and implicitly) inherits, provided it does not explicitly inherit from some other class.
  • If a class definition is qualified with the final keyword, this means that the class may not be used as a base class for inheritance.
  • When defining the body of a constructor for a derived class, a call to a constructor of the base class can be made, provided that call is the first statement in the body of the derived class. The syntax uses the keyword super as follows –


Java Arrays: Arrays are objects that store multiple variables of the same type. However, an array itself is an object on the heap.

Constructors and Initialization

We shall now discuss how constructors are “chained together” to build an object, and when instance variable initialization occurs. Now this rule has three parts, distinguished by the nature of the first statement in the constructor.

  • Firstly, if the first statement of a constructor is an ordinary statement (not a call to this or super), Java inserts an implicit call to super() to invoke the default constructor of the superclass. Upon returning from that call, Java initializes the instance variables of the current class, and then proceeds to execute the statements of the current constructor.
  • Secondly, if the first statement of a constructor is a call to a superclass constructor via super, Java begins by invoking that superclass constructor. Upon its return, Java initializes the instance variables of the current class, and then proceeds to execute the statements of the current constructor.
  • Lastly, if the first statement of a constructor is a call to an overloaded constructor via this, Java begins by invoking the selected constructor. Upon its return, Java simply proceeds by executing the statements of the current constructor. The necessary call to the superclass’s constructor has happened within the overloaded constructor, either explicitly or implicitly, so the initialization of instance variables has already occurred.

Polymorphism, Upcasting and Downcasting, and Shadowing

We will now define each one briefly,

  • Polymorphism refers to the principle that specific behavior (obtained by calling a specific method) can vary, depending on the actual type of the object. We can also say that it is the ability of different objects to respond to the same message (method call), but each in its own way.
  • Upcasting refers when an object of a derived class is assigned to a variable whose type is that of further up the same class hierarchy. However, this assignment does not require an explicit cast, since it is a “widening conversion”.
  • The term downcasting is used if we want to assign an object being referenced by a base class variable to a variable of a derived class type. In this case, the object must in fact be an object of the derived class type, and an explicit cast must be used.

We will now talk about Shadowing given the following scenario,

  • Shadowing superclass instance fields: When an instance field in a subclass has the same name as an instance field in its superclass, the field in the superclass is said to be shadowed by the one in the subclass.
  • Shadowing superclass static fields:  Static fields can also be shadowed, and the super syntax can be used for them as well. However, it is never necessary, since you can always refer to a class field with ClassName.fieldName in any case.
  • Overriding superclass instance methods: One of the important case, in which dynamic method lookup is used, and polymorphic behavior shows up. Whenever a class defines an instance method using the same name, parameter list, and return type as a method in its superclass, the method in the superclass is said to be overridden. Whenever the method is invoked for an object of the subclass, it is the new method in the subclass that is invoked, not the superclass version. If, in the body of the subclass method, you want or need to invoke the method with the same name from the superclass, you can do it using the super.methodName() syntax.
  • Overriding superclass static methods: Static methods are simply shadowed, not overridden. Another way of putting this is that static methods that are redefined in a subclass with exactly the same signature as in the superclass do not exhibit polymorphic behavior. Note .

Abstract Classes and Interfaces

An abstract method is a method defined by its signature, but having no method body. Correspondingly, an abstract class is a class that contains at least one abstract method, together with instance fields (data members) and implemented methods. Additionally, Java has a keyword abstract, which must be used as a qualifier for abstract classes. Also, a subclass of an abstract class may be instantiated, provided all abstract methods are implemented in the subclass. Otherwise, it too must remain abstract.


On the other hand, interfaces are kind of like a “pure abstract class”, in which any method must be abstract. Specifically, an interface can also have data fields, which are implicitly static and final. So, an interface may contain only abstract methods and/or constants.Unlike C++, Java does not permit multiple inheritance, but we can accomplish the same effect by a combination of single inheritance and “implementing” one or more “interfaces”.

Moreover, interfaces are a data type, such that when a class implements an interface, instances of that class can be assigned to variables of the interface type. This has the convenient and useful effect of allowing a single object to be considered as having more than one type. Polymorphism applies in this situation as well. Additionally, an interface can extend another interface in the same way that a class can extend another class. .

Boost your chances for better job opportunities. Practice and Prepare for Mobile Testing (Appium) Exam Now!

Go back to tutorial

Share this post
Quality Circles

Get industry recognized certification – Contact us