Tag Archives: Java

String Formatting using format in Java

Introduction We usually use System.out.println or System.err.println to print messages on the console. But to use the printf formatting options, we can use the System.out.format methods or System.err.format methods. Formatting output with System.out.format Formatting output using above methods will eliminate the plus signs we otherwise use to merge the values together and make the code more readable and… Read More »

Java String to Float

Introduction Many beginners want to know how to convert Java String to Java float. All String operations are easy in Java, and there are many utility methods. Java 6 onwards also has Float objects instead of the primitive float type, making float manipulations easier. How to convert Java String to Java float We will show an example to… Read More »

Java ternary operator examples

Introduction Java ternary operators make the code short and easy to understand. They can replace an entire set of if..else..then statements and also perform a lot of logical (Boolean) operations on the right-hand side of the statement in less amount of code. The operators work on Boolean expressions, i.e., either a Boolean field or statement that gives a… Read More »

Java try catch block

The try is a keyword that is used by java to enclose the code that might throw an exception. You can use the try block within the method body. It is recommended to keep that code in the try block only that will throw an exception. Also, the try block must always be followed by the catch and… Read More »

List in Java

Introduction Let us discuss multiple ways of adding the values of a list of integers. The traditional method to get the sum of a list in Java is to use the ‘for’ loop, but that’s a bit costly. Many utility methods can do the same with much grace. Java 8 also defines a new method, which we will… Read More »

Java int array

Introduction In Java, int arrays are used to store integers. In this article, we will see the different ways in which we can create and manage integer or int arrays. Before that, let us explain what is declaration, initialization, and populating array values. Declaring an int array: indicates that we want to create an array of integer values.… Read More »

Java Enum Example

Introduction Java Enums are used to define constants. Rather than declaring common categories of constants as individual strings, we can categorize and declare them as Enums. In this article, we will discuss how to declare Enums, use Java Enum in a for loop and Java switch case, and how Java Enums can be used in if/else statements. Some… Read More »