Best Way to Learn Java for a Beginner

Here you will know what is the best way to learn java for a beginner. Programming is the new vogue and a vogue which can make you earn wonders and at the same time make you utilize the capabilities of your brain to the fullest. Learning new programming languages however can sometimes turn out to be challenging especially when the language is one which holds immense importance in various fields. Java is definitely one of those languages which most of

Read more

Covariant Return Type in Java

In this article, we will learn about covariant return types in Java. Before Java version 5.0, we could not change the return type of an overridden method but a new method was introduced in JDK 5.0, called covariant where we could change the return type of the overridden method. The return type must be non-primitive. (i.e., it should be a sub-type of the overridden class). Program:

Output:

Explanation: In the above program, we created three classes: class ‘Employee’,

Read more

Static Import in Java with Example

In this article, we will discuss static import in Java. Just as import statements are used to access a class without its package qualification. In a similar manner, static imports are used to access static members without their class qualification. Some basic static methods used with the class names are as follows: Math.sqrt(4): It is used to find the square root of the given number. Math.max(4, 2): It is used to find the maximum of the given numbers. Math.min(4, 2):

Read more

Advantages and Disadvantages of Generics in Java

Generics in Java program was introduced in Java version 5. It is used to ensure the data type safety of objects. So, it constructs the code steady by detecting bugs at compile time. Why do we need generics in Java? When we write programs, errors occur. Some errors occur at run time while some errors occur at compile time. Those errors which occur at compile-time can be easily detected and rectified in the program. But, run time errors occur when

Read more

Nested Switch in Java

In this article, we will discuss about nested switch in Java with examples. When a switch statement contains further switch statements inside it, then it is called a nested switch. Inside the nested switch statement, we can give multiple cases and a default case. The inner switch statement must be part of any outer case statement. We cannot use the inner switch statements outside the cases.

Read more

TreeSet Vs TreeMap in Java

TreeSet is an implementation class of NavigableSet. In a TreeSet, duplicate as well as heterogenous (values of different data types) values are not allowed. If you’re trying to insert heterogeneous objects then it displays a ClassCastException. If the tree set exists, then we can insert a null value, and if the treeset doesn’t exist then we cannot insert a null value in it. All versions of Java below 6.0 allows insertion of null value, but all other versions show Null

Read more

How to Access Variable From Another Class in Java?

In this article, we will learn how to access variable from another class in java. There are two ways to get variables from another class. Create an object of another class in the main class Extend another class in the main class Let us take a look at both these methods one by one with the help of sample programs. Method 1: Create Object of Another Class in Main Class In the following example, to access the variable ‘a’ of

Read more

How to Get Filename Without Extension in Java

This article will talk about how to get filename without extension in Java language. The file utility method helps you to get rid of the file extension from the file name. Here is an example. Program:

Output:

Explanation: We take a file named ‘myfile’ with the extension ‘.java’. Next, we check whether the filename contains ‘.’ or not. If it contains ‘.’, then go to the else part and get filename without an extension using substring() function. This

Read more

Java Global Variables

In this article, we will see what is a global variable and how we can use this in our Java programs. In Java, all the functions and data members are declared inside a class and every declaration has to be done inside it. If we declare them outside the class, then they would not be accessible by the object of that class. Therefore, in Java, there is no such precise concept of global variables because these are described at the

Read more
1 2 3 24