Category Archives: Java

Foreach in C++ and Java

Foreach loop is used to access elements of an array quickly without performing initialization, testing and increment/decrement. The working of foreach loops is to do something for every element rather than doing something n times. There is no foreach loop in C, but both C++ and Jave support foreach type of loop. In C++, it… Read More »

How to swap or exchange objects in Java?

How to swap objects in Java? Let’s say we have a class called “Car” with some attributes. And we create two objects of Car, say car1 and car2, how to exchange the data of car1 and car2? A Simple Solution is to swap members. For example, if the class Car has only one integer attribute… Read More »

Flexible nature of java.lang.Object

We all love the mechanism of python, where we don’t have to bother about data types of the variables (don’t we!) Interestingly we have one class in Java too, which is pretty similar ! Yes, you guessed it right! It’s java.lang.Object For example, Output: java.lang.Character java.lang.Integer java.lang.String java.lang.Double java.lang.Boolean Such a behaviour can be attributed… Read More »

Redirecting System.out.println() output to a file in Java

System.out.println()  is used mostly to print messages to the console. However very few of us are actually aware of  its working mechanism. System is a class defined in the java.lang package. out is an instance of PrintStream  , which is a public and static member of the class System. As all instances of PrintStream class… Read More »

Infinity or Exception in Java when divide by 0?

Consider the following code snippets: Output: Infinity Output: Exception in thread “main” java.lang.ArithmeticException: / by zero at Geeksforgeeks.main( Explanation: In the first piece of code, a double value is being divided by 0 while in the other case an integer value is being divide by 0. However the solution for both of them differs. In… Read More »

User-defined Custom Exception in Java

Java provides us facility to create our own exceptions which are basically derived classes of Exception. For example MyException in below code extends the Exception class. We pass the string to the constructor of the super class- Exception which is obtained using “getMessage()” function on the object created. Output: Caught GeeksGeeks In the above code,… Read More »

Java is Strictly Pass by Value!

Consider the following Java program that passes a primitive type to function. Output: 5 We pass an int to the function “change()” and as a result the change in the value of that integer is not reflected in the main method. Like C/C++, Java creates a copy of the variable being passed in the method and… Read More »

Widening Primitive Conversion in Java

Here is a small code snippet given. Try to Guess the output At first glance, we expect “YOLO” to be printed. Actual Output: “YO155”. Explanation: When we use double quotes, the text is treated as a string and “YO” is printed, but when we use single quotes, the characters ‘L’ and ‘O’ are converted to… Read More »

String vs StringBuilder vs StringBuffer in Java

Consider below code with three concatenation functions with three different types of parameters, String, StringBuffer and StringBuilder. Output: String: Geeks StringBuilder: Geeksforgeeks StringBuffer: Geeksforgeeks Explanation: 1. Concat1 : In this method, we pass a string “Geeks” and perform “s1 = s1 + ”forgeeks”. The string passed from main() is not changed, this is due to… Read More »