Skip to main content

Posts

Java Switch keyword

Java switch keyword is used to provide multiple branching in a program. It tests the value of a variable and compares it with multiple case values. If there is a match, the associated block of code is executed. The break keyword is used to terminate the switch statement and control passes to the next line of code after switch. If none of the cases match the value of the variable, then the default label is executed. The default label must be present in every switch statement. If there is no default label, and if none of the cases match, then no action takes place and control passes to the next line of code after switch. The syntax for using Java switch keyword is given below: switch(expression) { case value1: //if expression == value1 //execute this block break; //terminates this particular case case value2: //if expression == value2 //execute this block break; //terminates this particular case //you can have any number of cases in a switch statement default: //executes this block if no...

Java Collections Made Simple

 Java Collection is a collection of classes and interfaces that makes data structure implementation easier. Examples of collection classes are. ArrayList,  LinkedList,  Hash Set,  Tree Set,  Queue,  Maps Collection Interface Java Collections are an essential part of Java programming, boosting the way we handle groups of objects.  They offer built-in methods that make it easy to manipulate data, improving both flexibility and performance.  Have you ever struggled with managing arrays or lists in Java? Java Collections solve this problem with ease. In this post, you'll explore how Collections simplify common tasks like sorting and searching.  You'll learn how to create and use different types of Collections through practical examples.  Whether you're a beginner or a seasoned developer, understanding Java Collections is crucial for writing efficient code. Curious about how it all works?  Let's dive into some code to make it clear and...

java ArrayList

Are you diving into the world of Java and wondering how to handle collections of data?  Look no further than the Java ArrayList. It's one of the most commonly used tools for managing and manipulating an array of objects.  But what exactly is an ArrayList, and how does it differ from other collections?  Let's explore the ins and outs of this essential Java class. What is an ArrayList in Java? An ArrayList in Java is a resizable array implementation of the List interface.  Unlike standard arrays, which are fixed in size, an ArrayList can grow and shrink dynamically.  Imagine it as a stretchy list that expands or contracts based on what you put inside.  This flexibility makes it an ideal choice for many applications. Why Choose ArrayList Over Arrays? Why would you use an ArrayList over the traditional array? Here's the deal: Dynamic Sizing : Arrays have a fixed size. Once you create them, you can't change the size. ArrayLists, however, adjust on the fly....

java Integers

 The class Integer wraps an object value of primitive type of int value. The object that is created from this class Integer will only hold a single value of primitive type int. Constructor of Integer class is as shown Integer(int value)

Java Characters

 To store a single a single character we use char data type. The character has to be surrounded by single quotes, for example 'x'. To create a character data type we do the following  char sex = 'm'; char sex = 'f'; m and f refer to male and female  Character Methods Character wrapper class has some important methods that can be used to work with char data type. toString(char char); This method takes character value and returns a String object , which is, a one-character string. String myChar=Character.toString('x'); System.out.println(myChar); toLowerCase(char c); This method takes in argumment of type char and return it to Lower case. System.out.println(toLowerCase('c')); toUpperCase(char c); This method takes in argumment of type char and return it to upper case. System.out.println(toUpperCase('c'); isLetter(char ch); This method takes in char argument and returns a Boolean whether is the argument true or not. public...

Boolean in java

 Boolean is a primitive data type, means it can only store a single value. Boolean data type can store true or false at a given time.  Example bellow shows how you can store and retrieve Boolean  data type. boolean isNight = true; boolean isMonday = false; The above sample code will store true and false respectfully. We usually use Boolean to evaluate conditions and make decision. Example code bellow try's to express the usage. class TrueFalse {      public static void main(String[] args){         int value1 = 1;         int value2 = 2;         if(value1 == value2)             System.out.println("value1 == value2");         if(value1 != value2)             System.out.println("value1 != value2");         if(value1 > value2)             System.out.println("value...

Java Hello World

Before writing your first Java program, make sure you have the following: Java Development Kit (JDK): This essential software is needed to write and run Java programs. You can download it from Oracle’s website . Text Editor or Integrated Development Environment (IDE): While you can use basic text editors like Notepad, an IDE like IntelliJ IDEA or Eclipse provides helpful features that simplify coding. Once these are ready, you’re set to begin coding! Let’s jump into writing your first "Hello World" in Java. Writing Your First Java Program Setting Up Your Environment First, open your IDE or text editor. Create a new file with a .java extension. For this guide, let’s name our file HelloWorld.java . Writing the Code Here’s the simple Java code to print "Hello World": public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Let’s break this down line by line so it all makes sen...