Java Tutorial – 5 | Edureka

Java Tutorial – 5 | Edureka

FavoriteLoadingAdd to favorites

Take instructor-led Live class on Java Tutorial at:

Java468x60

The following topics were covered in this Java Tutorial:

Input and Output Streams, Arrays List, Methods in Arraylist, Traversing an Arraylist, Iterator, Enumeration, List Iterator, Scanner, Bufferedreader, Tokenizer, Hash Map and its methods, Hash Table and its Methods.

Input and Output:
Input is the data what we give to the program. Sometimes the program will ask you to enter some data through keyboard which is nothing but input.
Output is the data what we receive from the program in the form of result. E.g.: result displayed on the console window in case of java programs.

How input is read from keyboard?
We have System.in class which represents the keyboard. To read data from it, it should be connected to InputStreamReader. This InputStreamReader reads data from keyboard and send that data to BufferedReader. Now BufferedReader reads data coming from InputStreamReader and stores that data in buffer. It has got some methods so that data can be easily accessed.

Reading input from console:
Input can be read from console in 3 ways: BufferedReader, String Tokenizer and Scanner
BufferedReader:
It accepts only one input from a single line. If you want to give another input then it should be given in next line. Hence each input should be given in different lines. It has got mainly two methods to accept the input and perform some actions on that input:
int read() — takes character as input from keyboard and returns integer.
String readLine() –takes a string as input from keyboard and returns a string.
To convert the string into integer or any other data type we need to use methods like Integer.parseInt(data) for integer and so on.
StringTokenizer:
It can be used to give multiple inputs in a single line separated by a delimiter. Here delimiter can be a whitespace or comma.In this first BufferedReader will take entire line as input. Now StringTokenizer will use the input stored in BufferedReader and divide the input into token by using delimiter you have specified in the program. Here also we need to use the method Integer.parseInt(data) and so on for converting string to integer and other data types. StringTokenizer has got the method nextToken() to get the tokens of the string passed to it from BufferedReader.
Scanner:
It accepts multiple inputs from file or keyboard and divides into tokens. It has methods to accept different types of data type like int, float, double and so on where as a StringTokenizer does not have.
It has got the method next() to get the String, nextInt() for to get the integer type of input, and so on. So no need of using Integer.parseInt() to type cast the string to integer.
To take the input scanner should be connected to keyboard (System.in) directly. So scanner would be the best way to accept the input from keyboard.
Writing output to console:
There are 2 ways to write output to console:
System.out.print (String);
System.out.write (int);
What is the difference between System.out.print (String) and System.out.println (String)?
The difference System.out.print (String) prints the string in the present line and the cursor will be at the point where the string ends but when we use System.out.println (String), it prints the string in the present line and then moves the cursor to next line.
Array List:
An ArrayList class is a concrete implementation of List interface. It allows duplicate elements.
The difference between an ArrayList and an Array is that ArrayList can grow or shrink dynamically where as size of an array is fixed once it is created.
ArrayList is present in “util” package. The index of the elements of an ArrayList will start from zero. We use add(Object e) to add the elements to the ArrayList and get(int index) to retrieve the element from the ArrayList by specifying the index. The default capacity of ArrayList is: 10
How to trace elements of ArrayList?
We can trace the elements of ArrayList in 4 ways: For-each loop, Iterator, ListIterator, Enumeration.
HashMap:
You create a map in which you pass two kinds of values: one is key and the other is value. Using key you will put values in the HashMap and whenever you have to retrieve value we use the key to do it. Key acts as indexes.To retrieve element we use Iterator. We use Map.Entry to get the entire set of keys and values and store into the Iterator. By using this entry we get the values and keys. Hash map will give an unordered set of output.

Please write back to us at support@edureka.in or call us at +91-8880862004 for more information.

Visit –

source

By |2020-03-20T02:22:43+00:00March 20th, 2020|Java Video Tutorials|1 Comment

One Comment

  1. Soniya Gupta March 20, 2020 at 2:22 am - Reply

    Why is the default capacity of ArrayList 10?

Leave A Comment

*