Skip to main content

Posts

Showing posts from May, 2019

Java Array, Multi Dimensional Array, Anonymous Array and Arrays Util Class

Array An array is a index based container object. The length of an array fixed at time of array creation. An Array holds a fixed number of values of a single type . One Dimensional Array Array can be created by two ways. Syntex: //1st way arraytype[] name = new arraytype[length]; //2nd way arraytype name[] = new arraytype[length]; 1st way is more readable. Examples: //charecyter Array Example: //creating Array of char, length 5 char[] charArray = new char[5]; //Initialize array elements charArray[0] = 'H'; charArray[1] = 'E'; charArray[2] = 'L'; charArray[3] = 'L'; charArray[4] = 'O'; //creating Array of int, length 4 int[] intArray = new int[4]; //Initialize array elements intArray[0] = 2; intArray[1] = 0; intArray[2] = 1; intArray[3] = 5; //creating Array of string, length 7 String[] weeks = new String[7]; //Initialize array elements weeks[0] = "mon"; weeks[1] = "tue"; weeks[2] = "wed"; w

String - A story of very special class in java

String is most used java class in any java project. Java treated String as special class. Lets see, what so special in String. String Basics String is group of characters. char[] chrs = {'i',' ','a','m',' ','h','e','r','o','.'}; String str = new String(chrs); System.out.println(str); /* Output: i am hero. */ String is an immutable class. Immutable class:  A java class which object can not be modified if once created. Because String class is immutable, String is final in nature, its mean you can not modify string object once you created. Default value of string is null. Simplest way to initialize String is //object created in String Literal Pool String str = "hello world"; Above example known as String literal, All string literals in Java programs, such as "hello world", are object of String class. Another way to create String object is //object created in Heap m

Java Thread - Let's move to parallel execution.

Assume we have 5 array of integer type and we want to add all numbers of array. 1st way ( Sequential Way ) We can pick array one by one and add all numbers and give the result. In this way only one thread (main thread) will execute the program. 2nd way ( Multithreaded Way ) In Multithreaded way, we can create 5 thread and assign an array to each thread to add all the numbers of array. All 5 thread will run parallel and give result 5 times faster than 1st way. What is Thread & Multithreading? A process may contain sub processes which execute parallel and use same resource of main process these sub processes are threads and whole phenomena called Multithreading . How to implement thread in Java? Java thread is sub process of process. In java, Thread class use to achieve multithreading in program. Initialize Thread There is two way to initialize and run thread in java. By extending Thread class By implementing Runnable interface By extending Thre

Java I/O Stream - FileReader and Writer , BufferedReader and Writer

Java I/O Stream Input Stream: Taking input data from other source in your program. Output Stream: Your program giving some data to other source. Note: Other source can be another java program, console, file or any other program. Task 1st: Take input character from console and print it on console. Java input output classes belongs to java.io package and by default each and every class in java have java.lang package. java.lang package contains System class, which we use for print something on console like System.out.println("Hello World"); if you open System class src code, you will find system class has a PrintStream type variable with name out and this PrintStream class belongs to java.io package have method println(String arg) , which print passed argument in print method on console. I/O variables of System Class: System.out: use for giving output to console. Type: PrintStream System.in: taking input from console. Type: InputStream import java.io.IOEx