Found 338 Articles for Java Programming

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

717 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Assigning long values carefully in java to avoid overflow

karthikeya Boyini
Updated on 18-Jun-2020 15:24:22

192 Views

In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.ExampleLive Demopublic class Tester { ... Read More

Copying file using FileStreams in Java

Samual Sam
Updated on 18-Jun-2020 15:26:45

240 Views

This example shows how to copy the contents of one file into another file using read & write methods of FileStreams classes.ExampleLive Demoimport java.io.*; public class Main {    public static void main(String[] args) throws Exception {       BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));       out1.write("string to be copied");       out1.close();       InputStream in = new FileInputStream(new File("srcfile"));       OutputStream out = new FileOutputStream(new File("destnfile"));       byte[] buf = new byte[1024];       int len;       while ((len = in.read(buf)) > 0) { ... Read More

Conversion of Stream To Set in Java

karthikeya Boyini
Updated on 18-Jun-2020 15:28:33

2K+ Views

We can convert a stream to set using the following ways.Using stream.collect() with Collectors.toSet() method - Stream collect() method iterates its elements and stores them in a collection.collect(Collector.toSet()) method.Using set.add() method - Iterate stream using forEach and then add each element to the set.ExampleLive Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors;   public class Tester {            public static void main(String[] args) {       Stream stream = Stream.of("a", "b", "c", "d");       // Method 1       Set set = stream.collect(Collectors.toSet());       set.forEach(data -> System.out.print(data + " ")); ... Read More

Conversion of Set To Stream in Java

Samual Sam
Updated on 18-Jun-2020 15:31:03

599 Views

Being a type of Collection, we can convert a set to Stream using its stream() method.ExampleLive Demoimport java.util.HashSet; import java.util.Set; import java.util.stream.Stream; public class Tester {    public static void main(String args[]) {       Set set = new HashSet();       set.add("a");       set.add("b");       set.add("c");       set.add("d");       set.add("e");       set.add("f");       Stream stream = set.stream();       stream.forEach(data->System.out.print(data+" "));    }   }Outputa b c d e f

Conversion of Array To ArrayList in Java

karthikeya Boyini
Updated on 07-Nov-2023 03:12:20

50K+ Views

We can convert an array to arraylist using following ways.Using Arrays.asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.Collections.addAll() method - Create a new list before using this method and then add array elements using this method to existing list.Iteration method - Create a new list. Iterate the array and add each element to the list.Example import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Tester {    public static void main(String args[]) {       String[] array ... Read More

Compound assignment operators in Java

karthikeya Boyini
Updated on 18-Jun-2020 14:44:15

1K+ Views

The Assignment OperatorsFollowing are the assignment operators supported by Java language −OperatorDescriptionExample=Simple assignment operator. Assigns values from right side operands to left side operand.C = A + B will assign value of A + B into C+=Add AND assignment operator. It adds right operand to the left operand and assigns the result to left operand.C += A is equivalent to C = C + A-=Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to left operand.C -= A is equivalent to C = C � A*=Multiply AND assignment operator. It multiplies right ... Read More

Compilation and execution of Java Program

Samual Sam
Updated on 18-Jun-2020 14:46:45

16K+ Views

Let us look at a simple code first that will print the words Hello World.ExampleLive Demopublic class MyFirstJavaProgram {    /* This is my first java program.        * This will print 'Hello World' as the output        */    public static void main(String []args) {       System.out.println("Hello World"); // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to ... Read More

Comparison of double and float primitive types in Java

karthikeya Boyini
Updated on 18-Jun-2020 14:50:29

568 Views

If we compare a float and a double value with .5 or .0 or .1235 (ending with 5 or 0), then == operator returns true, otherwise it will return false. See the below example.ExampleLive Demopublic class Tester {    public static void main(String[] args) {       double d1 = 2.5;       float f1 = 2.5f;       System.out.println(d1 == f1);       double d2 = 2.4;       float f2 = 2.4f;       System.out.println(d2 == f2);    } }Outputtrue falseThe reason behind this logic is the approximation of float and ... Read More

Comparison of autoboxed integer object in Java

Samual Sam
Updated on 18-Jun-2020 14:52:30

203 Views

When we assigned an int to Integer object, it is first converted to an Integer Object and then assigned. This process is termed as autoboxing. But there are certain things which you should consider while comparison of such objects using == operator. See the below example first.ExampleLive Demopublic class Tester {    public static void main(String[] args) {       Integer i1 = new Integer(100);       Integer i2 = 100;               //Scenario 1:       System.out.println("Scenario 1: " + (i1 == i2));       Integer i3 = ... Read More

Advertisements