Posts

What is main Method in java ? how to use main method in Java ? can we overload main method ? why main method is static?

What is the main Method in java? how to use the main method in Java ? can we overload the main method? why the main method is static   What is the main Method in java? The main method is the entry point that means it is the starting of every java program. if JVM runs any program for java it first needs is find the main method in java. it is always written as public static void main(String[] args) . public : Public is an access modifier, which is used to define who can access this method that means who can use this method. Public means that this Method will be accessible by any Class. static : Static keyword used for memory management. It is a keyword in java that identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main () is called by the JVM before any objects are made and only static met...

How do I create a Java string from the contents of a file? convert file to Java String.

How do I create a Java string from the contents of a file? convert the file to Java String. The following a possible way for getting data from the file as a String form. Read all text from a file First Solution:- import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; String content = new String(Files.readAllBytes(Paths.get("readMe.txt")), StandardCharsets.UTF_8); Second Solution:- Guava has a method similar to the one from Commons IOUtils that Williams Rohr mentioned: import com.google.common.base.Charsets; import com.google.common.io.Files; // ... String text = Files.toString(new File(path), Charsets.UTF_8); Third Solution:- Java 11 added the readString() method to read small files as a String, preserving line terminators: String content = Files.readString(path, StandardCharsets.US_ASCII); For versions between Java 7 and 11, here's a compact, robust the idiom, wrapped up in a u...

How do I read / convert an InputStream into a String in Java? convert String to InputStream.

How do I read/convert an Input Stream into a String in Java? convert String to Input Stream. First, we learn What is InputStream? The  Java   InputStream  class,  java.io.InputStream , represents an ordered stream of bytes. you can read data from a Java  InputStream as an order sequence of bytes. Java stream is a flow of data from a source into a destination. The source or destination can be a disk, memory, socket, or other programs. The data can be bytes, characters, or objects. The following way of Converting String to InputStream. First Solution:- A nice way to do this is using  Apache commons   IOUtils  to copy them InputStream into a StringWriter... something like. StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding); String theString = writer.toString(); or // NB: does not close inputStream, you'll have to use try-with-resources for that String ...

How do I convert a String to an int in Java? convert String to java,Solution java.

First Solution:- String myString = "1234"; int foo = Integer.parseInt(myString); you'll notice the "catch" is that this function can throw a NumberFormatException, which of course you have to handle: int foo; try { foo = Integer.parseInt(myString); } catch (NumberFormatException e) { foo = 0; } (This treatment defaults a malformed number to 0, but you can do something else if you like.) Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int: import com.google.common.primitives.Ints; int foo = Optional.ofNullable(myString) .map(Ints::tryParse) .orElse(0); Second Solution:- Simply you can try this: Use Integer.parseInt(your_string); to convert a String to int Use Double.parseDouble(your_string); to convert a String to double Example String str = "8955"; int q = Integer.parseInt(str); System.out.p...

How To Create An Object In Java ?

In this article, you will learn how to create an object in java along with accessing instance variables and methods of a class through created object.  Let us first dive into the concepts of classes and objects. Class-  A class serves as a blueprint for creating objects. It contains properties(variables) and methods(functions) that determines the state or behaviour of an object. A class is created by declaring the keyword ‘class’ followed by the ‘name of the class’. For example: class  Cuboid { // class body } The body of the class is contained between two curly braces. Object-  An Object may be defined as the sample of a class showing state or behaviour. In simple words, an object is created from a class. CREATING A CLASS IN JAVA A class in java can be created by using the keyword class followed by the name of the class. The members of the class(properties and methods) are declared under the curly braces as shown below in the example. public cl...

How to get a value inside an ArrayList java ?

Retrieve value from ArrayList in java How to get Value from ArrayList how to add new ArrayLists and ADD value to that particular ArrayList and how to RETRIEVE the data form that list. ArrayList < ArrayList < Integer >> arrayList = new ArrayList < ArrayList < Integer >>(); ArrayList < Integer > newAL = new ArrayList < Integer >(); arrayList . add ( newAL ); newAL . add ( 1 ); newAL . add ( 2 ); newAL . clear (); System . out . println ( arrayList . get ( 0 )); //Changes persist in your arraylist So after adding ArrayList you can manipulate  newAL  as  ArrayList   stores reference  you don't need to fetch and set element from main  arrayList . To retrive data you can  Iterate (Use  ForEach Loop ) over  arrayList  or you can do following Integer List0Item1 = arrayList . get ( 0 ). get ( 1 ); //Get first element of list at 0 arrayL...