Welcome to Java!

Welcome to Java!

I recently re-learnt Java, presenting a basic, quick language overview.

Part 1:The basics.


What is Java and JVM?

Java is an Object Oriented language developed in the early 1990s by James Gosling. It is robust, secure, easy to use and platform-independent. It also has an in-built garbage collector.

JVM or Java Virtual Machine is a virtual machine that allows Java to become platform independent. It is a part of JRE(Java Runtime Environment). It converts code to Java bytecodes which can be run on any system irrespective of the operating system as long as the system has a JDK(Java Development Kit) installed.

Input/Output in Java

In Java, we can use 2 main classes to receive input from the user: BufferReader and Scanner class.

  1. BufferReader: This class simplifies the process of taking input by reading text from the character input stream. It can be used with FileReader("note.txt"). It has

    built-in functions and provides a buffer when taking input. To read another stream of data we use InputStreamReader.

    BufferReader br=new BufferReader(new InputStreamReader(System.in));

    br.readLine();

    BufferReader is great at inputting string as it reads the line as it is and thus is a fast way of processing input. To scan other data types like int, double etc we use parseInt(), parseDouble(), and split() with BufferReader. It thus requires more typing and can reduce readability. It also needs the programmer to handle the IOException on his own. It is most suitable for fast input, and line reading.

Scanner: Scanner class makes the process of taking input much easier. It reads data in the form of tokens and has built-in functions to read different data types.

Scanner scanner = new Scanner(new File("note.txt"));

Scanner scanner = new Scanner(System.in);

nextLine(): Reads the current line and moves the scanner's position to the next line.

nextInt(): Reads token value as an integer.

nextDouble(): Reads token value as a decimal.

Control statements

The condition and iteration statements used in JAVA have keywords common among many other programming languages.

  • if else: if something is true, follow a particular block of code, else follow some other block of code.

  • switch: check for the value stored in a variable and depending on equality, run a particular case.

  • loops: repeat a block of code, a certain number of times. types: for loop, while loop, do-while loop.

  • Continue: Makes control jump directly to the beginning of the loop, ignoring the statements below it.

  • Break: When a break statement is encountered, it terminates the loop.

  • Return: return is a reserved keyword, used to exit a method with or without a value.

Operators in Java

Java offers a variety of operators, which can be classified as follows:

  • Arithmetic Operators: used to perform basic mathematical operations. These include:

    1. + (addition)[adds two numbers],

    2. - (subtraction)[subtracts two numbers],

    3. * (multiplication)[multiplies two numbers],

    4. / (division)[divides two numbers] and

    5. % (modulus)[returns remainder after division of two numbers].

  • Unary Operators: these operators require only one operand. These include:

    1. - (Unary minus)[converts a positive value to a negative one and visa versa]

    2. ! (NOT operator)[It changes the value of a boolean expression. basically true becomes false and false becomes true]

    3. ++ (Increment operator)[Increases the value of an integer by 1. it can be used as pre-increment(++a) or post-increment(a++)]

    4. -- (Decrement operator)[Decreases the value of an integer by 1. it can be used as pre-decrement(--a) or post-decrement(a--)]

    5. ~ (Bitwise)[It reads the value in binary system and then returns the 2's complement of that number]

  • Assignment Operator: This operator is used to assign a value to a variable. The value written on the right of this operator gets stored in the variable written on the left of this operator. Both, the value and the variable should have a compatible data type.

    1. \= [a = 10. value 10 gets stored in a, if a is an integer]

    2. += [a += 10. This means a = a+10. value 10 gets added to a previously stored value in a, which is then saved in a]

      Similarly, other Arithmetic operators can be paired with the assignment operator like -=, *=, /=, and %=.

  • Relational Operators: These check the relation between two operands. These are:

    1. \== (Equal to operator)[This check for equality. If the value on the right-hand side is equal to the value on the left-hand side, it returns true. Otherwise, it returns false]

    2. != (Not equal to operator)[This checks against equality. If the value on the right-hand side is equal to the value on the left-hand side, it returns false. Otherwise, it returns true]

    3. \> (Greater than operator)[If the value on the left-hand side is greater than the value on the right-hand side, it returns true. Otherwise, it returns false]

    4. < (Less than operator)[If the value on the left-hand side is lesser than the value on the right-hand side, it returns true. Otherwise, it returns false]

    5. \>= (Greater than or equal to operator)[If the value on the left-hand side is greater than or equal to the value on the right-hand side, it returns true. Otherwise, it returns false]

    6. <= (Less than or equal to operator)[If the value on the left-hand side is lesser than or equal to the value on the right-hand side, it returns true. Otherwise, it returns false]

  • Logical Operators: These are used to perform logical operations, based on results of boolean expressions the control of code is continued.

    1. && (Logical AND)[a&&b, if a is true and b is also true, then the overall condition is true]

    2. || (Logical OR)[a||b, if either a is true or b is true, overall condition becomes true]

    3. ! (Logical NOT)[!a, if a is true, return false and visa versa]

In the above statements, a and b used are probably going to be relational statements. example: a=10>20 and b=4<=5.

  • Ternary Operator: It is a conditional operator and requires 3(or more) operands. It can be used to check a condition and based on whether the condition is true or not, the output expression is decided. looks like (c1)?(e1):(e2)

    a= c>b?c:b;

    if (c>b) comes true, value of c will be stored in a, else value of b will be stored in a.

  • Bitwise Operator: Bitwise operators are used to manipulate the individual bits of a number. They are mainly used to do update and query operations of the Binary indexed trees.

    1. |(Bitwise OR)[This operator returns bit by bit, OR of the bits of input values, that is if either of the bits is 1, it gives 1. Otherwise, it gives 0.]

    2. &(Bitwise AND)[This operator returns bit by bit, AND of the bits of input values, that is if both of the bits are 1, it gives 1. Otherwise, it gives 0.]

    3. ^(Bitwise XOR)[This operator returns bit by bit, XOR of the bits of input values, that is if both the bits are different, it gives 1. Otherwise, it gives 0.]

    4. ~(Bitwise complement)[This operator reversed all bits and returns it, that is one becomes 0 and 0 becomes 1]

Example: 5 and 7 are 0101 and 0111 in binary. then 5|7 is 0111(7), 5&7 is 0101(5), 5^7 is 0010(2), ~5 is 1010(10).

  • Shift Operator: Shift operator performs bit manipulation on data. It shifts the bits depending on the direction specified.

    1. <<(Left shift)[The left shift operator moves all bits by a given number of bits to the left. for example 2(0010) will become 8(1000)]

    2. \>>(Right shift)[The right shift operator moves all bits by a given number of bits to the right. for example 8(1000) will become 2(0010)]


Thank you :)