Generic class, interface and generic method
In the post generics in Java basics of Java generics are already covered. In this post we’ll see how to create generic class, generic method and generic interface. Generic classA generic class is...
View ArticleBounded type parameter in Java generics
In the post generics in Java you would have already seen example where type parameters are replaced by any class type. But there are times when you want to restrict the types that can be used as type...
View ArticleSplitting a String - Java Program
In many applications you get data in a text file which is either separated using a pipe (|) symbol or a tab symbol (/t). Now, if you want to do a quick split around that separating symbol you can...
View ArticleWildcard in Java Generics
In Java generics there is also an option of giving question mark (?) called wild card as type. This question mark(?) represents an unknown type.The wildcard can be used in a variety of situations -As...
View ArticleObject creation using new operator in Java
In OOP a class provides the blueprint for objects; you create an object from a class. When a new class is created essentially a new data type is created. This type can be used to declare objects of...
View ArticleHow to count lines in a File - Java Program
Sometimes you just want to count the number of lines in the file, rather than reading the file and performing some logic. The easiest way, I feel, is to use LineNumberReader for counting the lines....
View ArticleType erasure in Java Generics
When generics was introduced in Java there was a requirement for it to be compatible with the existing code, written in previous versions, which of course was non-generic. That is why you can still add...
View ArticleSetting and getting thread name and thread ID - Java Program
If you want to set a thread’s name in order to identify a thread that can be done in 2 ways. Using the constructor of the Thread classUsing the setName() method Thread IDAnother way to uniquely...
View Articleinstanceof Operator in Java
In your application sometimes you have a situation when you would like to know the type of an object during run time. It is evidently important when you have a parent-child relationship, an inheritance...
View ArticleType Casting in Java
Whenever you try to assign data of one type to variable of another type conversion happens, it may be a - Widening primitive conversionsWidening reference conversionsNarrowing primitive...
View ArticlePrimitive Data Types in Java
The Java programming language is statically-typed meaning all variables should first be declared before they can be used. So you need to state variable's type and name, as exampleint test = 1;Doing so...
View ArticleKinds of Variables in Java
As mentioned in the post object in Java, object is an instance of the class, which gets its state and related behavior from the class. At the same time an object stores its state in variables.So in...
View ArticleLiterals in Java
When you initialize a variable of a primitive data type in Java, you don’t use the new keyword. It is because primitive types are special types built into the language, they are not the objects created...
View ArticleVolatile in Java
If volatile is to be defined in simple terms, it can be defined as “the volatile keyword is an indication that the variable marked as volatile can change its value between different accesses”.In order...
View ArticleEnum Type in Java
An enum type is a special data type that helps you to define a list of predefined constants which can be accessed using a variable. An enumeration is created using the enum keyword.As example, if you...
View ArticleProducer consumer Java Program using volatile
Producer-consumer problem using multiple threads is a frequently asked multi-threading interview question. Though there are many ways to do it like - Producer-Consumer Java program using wait...
View ArticleComparing Enum to String - Java Program
Sometimes you may have the scenarion where you want to compare String to enumeration constants. Like you may have an enum with product codes an you want to check that the passed produce code is one of...
View ArticleConverting String to enum type - Java Program
There may be a scenario where a string is passed in your code and you have to convert it into the enum type. For that you can use valueOf() method which is implicitly created for all enums.public...
View ArticleTypeWrapper Classes in Java
As explained in the post primitive data types in Java there are eight primitive data types and most of the time you will use the primitive types in your code as it reduces the object creation overhead...
View ArticleAutoBoxing and UnBoxing in Java
From Java 5 two new features were added Autoboxing and Unboxing where Autoboxing automates the wrapping of primitive data types into the wrapper class for that particular data type when an object...
View Article