Category Archives: Java

Loops in Java

Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time. while loop: A while loop is a… Read More »

Simple Calculator using TCP in Java

Prerequisite: Socket Programming in Java Networking just doesn’t conclude with a one-way communication between the client and server. For example consider a time telling server which listens to request of the clients and respond with the current time to the client. Real-time applications usually follow a request-response model for communication. Image Source: As depicted… Read More »

Method Overloading and Ambiguity in Varargs in Java

Prerequisite – Varargs , Method Overloading Method Overloading in Varargs Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-length argument by following ways: Case 1 – Methods with… Read More »

Java.lang.Number Class in Java

Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful methods which are used frequently while dealing with numbers. These… Read More »

Output of Java programs | Set 9 (Garbage Collection)

Difficulty level : Intermediate In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection. Predict the output of following Java Programs: Program 1 : Output: end of main… Read More »

Java Identifiers

In programming languages, identifiers are used for identification purpose. In Java an identifier can be a class name, method name, variable name or a label. For example : public class Test { public static void main(String[] args) { int a = 20; } } In the above java code, we have 5 identifiers namely : Test : class name.… Read More »

Searching characters and substring in a String in Java

Searching a character in the String indexOf(char c) : It searches the index of specified character within a given string. It starts searching from beginning to the end of the string (from left to right) and returns the corresponding index if found otherwise returns -1. Note: If given string contains multiple occurrence of specified character… Read More »

strictfp keyword in java

strictfp is a keyword in java used for restricting floating-point calculations and ensuring same result on every platform while performing operations in the floating-point variable. Floating point calculations are platform dependent i.e. different output(floating-point values) is achieved when a class file is run on different platforms(16/32/64 bit processors). To solve this types of issue, strictfp… Read More » Class methods

OutputStreamWriter class connects character streams to byte streams. It encodes Characters into bytes using a specified charset. Declaration : public class OutputStreamWriter extends Writer Constructors : OutputStreamWriter(OutputStream geek_out) : Creates a “geek_out” OutputStreamWriter that uses a default charset for encoding. OutputStreamWriter(OutputStream geek_out, Charset geek_set) : Creates a “geek_out” OutputStreamWriterthat uses a “geek_ set” charset for… Read More »

Character Class in Java

Java provides a wrapper class Character in java.lang package. An object of type Character contains a single field, whose type is char. Creating a Character object : Character ch = new Character(‘a’); The above statement creates a Character object which contain ‘a’ of type char. There is only one constructor in Character class which expect… Read More »