You are here
Home > java > Core Java >

Java Core Tutorial

Java Core Tutorial

In this section we will cover the basics of Java with clear and simple explanation. Here in this section we have covered concepts in the form of questions. Moreover, I am sure that these concepts will be beneficial to new developers & to those who are preparing for interviews. We will call this section ‘Java Core Tutorials’. They are like must to know concepts for every Java professional. In addition, we have a separate section for Java Interview Questions. Let’s start learning basics of Core Java in our topic ‘Java Core Tutorial’.

Q: What is the history of Java ? How popular it is ?

In the early nineties, Java was created by a team led by James Gosling for Sun Micro-systems (Now Oracle). It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated web pages . Over the years it has evolved as a successful language for use both on and off the Internet. A decade later, it’s still an extremely popular language, with over 6.5 million developers worldwide. In the last more than 25 years Java is accepted by the majority of clients as the best language to develop applications. Java features specially Java 8 features make Java more popular among its clients and users.

Q: What are the features of Java ?

Ans : There are 12 important buzzwords(features) in java.

  1. Simple : Easy to learn, most of the difficult features like pointers, multiple inheritance concepts have been removed from Java.
  2. Platform independent : Write once run anywhere.
  3. Architecture Neutral : If you perform any changes in your architecture of system, java program will still run without any issue.
  4. Portable : We can migrate java code from windows to Linux easily.
  5. Secure : Lets you creating applications that can’t be invaded from outside.
  6. Object Oriented : From JDK 1.8 onwards Java has also incorporated functional(procedural) programming.
  7. Multi-threaded : You can build applications with many concurrent threads of activity, resulting in highly responsive applications.
  8. Robust : Having reliable programming habits for creating highly reliable applications.
  9. Distributed : It facilitates users to create distributed applications in Java. To create distributed applications we use RMI, EJB, CORBA etc .
  10. Interpreted : Java integrates the power of Compiled Languages with the flexibility of Interpreted Languages.
  11. High Performance : Java code after compilation converts to bytecode which is highly optimized by the Java compiler, so that the Java virtual machine (JVM) can execute Java applications at full speed.
  12. Dynamic : It supports dynamic loading of classes. It means class loading happens on demand.

Q. How is Java a platform independent language?

Ans : In general the compiler’s job in a programming language is to convert source code into machine understandable code( also called executable code). There is one more state of code between source code & machine understandable code in java which is called the byte-code. In fact converting java source code to machine understandable code is two step process. One is from source code to byte-code(.class file) which is done by compiler. Other is from byte-code to machine understandable code which is done by the JVM. However JVM is a software which comes automatically with JDK installation.

JVM is platform dependent. Therefore while converting from byte-code to executable code JVM makes it compatible with the platform to which it belongs. In this complete process there are two translators which makes this possible ie. compiler & JVM. Compiler can read source file(.java file). Similarly JVM can read byte-code(.class file). Because of the feature of platform in-dependency Java’s slogan in Write Once Run Anywhere(WORA). Also below points are important to keep in mind.

Java Source Code - platform independent
Java Bytecode - platform independent
Java Compiler - platform independent
Java Executable Code - platform dependent
Java Virtual Machine - platform dependent
Java Software(jdk) - platform dependent
Java Program - platform independent
Java Software Application - platform independent

The languages where conversion of source code to machine language is a single process, those languages come under platform dependent category like C, C++.

Q. What is JRE, JDK, JVM in java and what are the differences between them?

Ans: First of all let’s understand what are the usage of these terms in java. JRE(Java Runtime Environment) helps us to run the compiled code in java whereas JDK(Java Development Kit) helps us to compile & run the code. Hence if you want to run already compiled code such as a code in the form of jar/war/ear, only JRE is sufficient. On the other hand if want to compile & then run your code, you must have JDK in your system. JVM(Java Virtual machine) helps us to convert compiled code(byte code) into machine language code. JRE contains JVM and JDK contains JRE. 

JRE = Java API + JVM
JDK = Compiler + JRE
JVM = Interpreter + JIT

JVM runs java program with the help of interpreter.
JIT (Just in time compiler) helps interpreter to run fast when its slow.
Java API carries predefined programs like String, System, Thread, Exception, FileInputStream, FileOutputStream, ArrayList, Collections etc….

Q. What is the JIT compiler?

Ans: When the program finishes its execution, the JIT compiler runs and compiles the code into a faster form without interpreting it. Furthermore, a standard compiler can’t access the dynamic runtime information, whereas the JIT compiler does and can make the better optimizations as well.

Q. What makes Java performant?

Ans: The Just-in-Time(JIT) compiler makes Java performant. Typically, it involves translating bytecode into machine code and then executing it directly instead of interpreting it. For enabling high performance, Java can make use of the Just-In-Time compilation. The JIT compiler is enabled by default in Java and gets activated as soon as a method is called. It then compiles the bytecode of the Java method into native machine code. After that, the JVM calls the compiled code directly instead of interpreting it. In this way, it makes Java performant.

Q. Where is Java being used?

Ans: Java is the most widely used language. The following are some application areas where we find Java usable:

1) Desktop applications
2) Web applications
3) Mobile applications (Android)
4) Cloud computing
5) Enterprise applications
6) Operating Systems
7) Web servers and application servers
8) Embedded systems
9) Computer games
10) Scientific applications
11) Smart cards
12) Cryptography

Q. What are the variables in Java and their types?

Ans: Variables are placeholders in java. In other words, Java stores the program data in variables.

They are of 4 types in Java:

1. Instance Variables (Non-Static Fields)

2. Class Variables (Static Fields)

3. Local Variables

4. Parameters

♥ Some people don’t consider parameters as a variable, but it comes under the type of variable as per Oracle java documentation.

Q. What is Data Types in Java?

Ans: In Java every variable is declared with the help of a data type. The data type of a variable determines the type of data the variable can contain, and what operations we can execute on it. Furthermore, there is a basic java data type for each variable in Java. There are two types of data types: Primitive Data Types, Non-primitive Data Types

Primitive Data Types

There are 8 types of primitive data types in Java:
int
float
char
boolean
byte
short
long
double

Non-Primitive Data Types

These are the special data types that are user-defined. They are also called reference data types. For example, classes, interfaces, String, arrays, etc. are the Non-primitive data types.

Note : ‘var’ is a case sensitive type identifier that helps the java compiler to infer the values, introduced in Java 10 version. However, it is not a keyword. We can only use them in case of local variables. Moreover, we can use var anywhere in java except in class name. You may visit the link for examples of var.

Q. What is the typecasting in Java?

Changing the data type of a variable is typecasting. In fact, it is not possible for the boolean data type. It is of two types: Implicit and Explicit

Implicit Typecasting: Changing from a smaller data type to the larger data type. The compiler does this process automatically.

Explicit Typecasting: Changing a larger data type into a smaller data type. This results in information loss:

Q. What are operators in Java?

Ans: Like any other language, Java also has some set of operators to perform specific programming operations.

There are the following types of Java operators;

1) Arithmetic Operators
2) Logical Operators
3) Unary Operators
4) Ternary Operators
5) Assignment Operators
6) Relational Operators
7) Bitwise Operators
8) Shift Operators
9) instanceOf operator

Q. What is comment in Java?

Ans: Comments improve code readability and understandability. Whenever we need to add documentation about the functionality of the code, we should add a comment. However, the Java compiler never reads the comments and simply ignores them during execution.

The comments are of two types:

Single-Line Comments 

As the name suggests, this comment contains a single line. Generally, we write it after a code line to explain its meaning. We represent the single-line comments with two backslashes(//). For example, observe the code below:

public class SingleLineComment {

   public static void main(String[] args) {

   //Initialising str with value: ”javatechonline.com”
   String str = “javatechonline.com
   }
}

Multi-Line Comments 

It contains multiple lines. Generally, we write to them at the beginning of the program as a short explanation of the program. Developers also use them to comment out blocks of code during debugging. We mark them using starting tag(/*) and an ending tag(*/).

public class MultiLineComment {

 public static void main(String[] args) {

 /* String str = "javatechonline"
 String str1 = str.concat(".com");
 System.out.println(str1);
 */

 }
}

Q. What is method in Java?

Ans: Methods are the most important part of a program in Java. This is the place where a programmer writes the logic to create a functionality of the program. In Java, generally there are two types of methods:

  • User-defined Methods: We can create our own method based on our requirements to implement a functionality.
  • Standard Library Methods: These are built-in methods in the Java library that we can use. In fact, sometimes we use these methods while writing a user-defined method.

Q. How to write a Java Method?

Ans: The simplest syntax to declare a method is:

returnType methodName() {
  // method body
}

Here,

Return Type: The return type specifies what type of value a method returns. For example, if a method has an int return type, then it returns an integer value. If the method does not return a value, its return type will be void.
Method Name: It is an identifier that we used to refer to the particular method in a program.
Method Body:  It includes the logic in the form of programming statements to perform some tasks. The method body is enclosed inside the curly braces { }.

Furthermore, the complete syntax of declaring a method is as below:

specifier static returnType nameOfMethod (parameter1, parameter2, ...) {
  // method body
}

In the above syntax,

specifier – It defines access types whether the method is public, private, protected, and default.

static This is also a modifier/specifier. There are several modifiers that may be part of a method declaration such as static, final, abstract, synchronized, strictfp, native.

parameter1/parameter2 – These are values passed to a method. We can pass multiple number of arguments to a method based on our requirement.

Q. What is String Pool in Java?

Ans: String pool is the set of strings stored in the heap memory. Whenever a new object is created, it is checked if it is already present in the String pool or not. If it is already present, then the same reference is returned to the variable else new object is created in the String pool, and the respective reference is returned. It is always advisable to create strings in the String pool whenever it is possible in order to save the memory.

Q. What is the concept of boxing, unboxing, autoboxing, and auto unboxing.

Answer: These are concepts of interchanging between primitive value and object.

Boxing: The concept of getting an object from the primitive value.
Unboxing: Getting the primitive value from the object.
Autoboxing: Assigning a value directly to an integer object.
Auto unboxing: Getting the primitive value directly into the integer object.

public class BoxingUnboxing {
   public static void main(String args[]){
      int k = 4;
      Integer i = new Integer(k);   //Boxing
      Integer j = k;                //Unboxing
      int x = j.intValue();         //Unboxing
      int y = j;                    //AutoUnboxing
   }
}

Explore other topics from the Links below:

Java Features Before Java 8

Java 8 features

Functional Interfaces 

The Lambda (λ) Expression

Default Methods inside Interface

Static Methods inside Interface

Method Reference(::) 

Java Features After Java 8

Java 14 Features

JVM ARCHITECTURE, Class Loaders & Java Code Processing

close

Leave a Reply

Top