Welcome to the second part of our Core Java series. In the last part, we discussed the history of Java, it’s environment, how we can compile/run a Java program and variables. If you are new, please checkout part one of this series.
Let’s discuss some more aspects of Java Language.
If a variable value is fixed and can’t be changed it is called a constant. In Java, we use final
keyword to define a constant.
final int MAX = 10000;
This is used when we want to make sure that the value of a variable will not be changed during the runtime of a program.
Explicit data values that appear in a program are called literals.
e.g. int number = 25;
Here, number is variable
and 25 is literal
.
Comments are non-executable statements that are ignored by the compiler. They are used just to increase the readability of the program.
//
is used for a single line comment.
// this is a single line comment comment
/* */
is used for multiline comments.
1 2 3 4 5 6
/* This is a multiline comment */
We can declare a variable anywhere in the Java program just before it is used.
We can spread a single declaration over several lines. For example:
1
2
3
int height = 178,
weight = 60,
age = 25;
An expression is known as a valid combination of operands and operators. There are two types of expressions -
Constant Expression - All of the operands are either literals or constants. It is evaluated at compile time (when we compile the code using Javac).
e.g.
2 + 3
10 * 40 + 4
int a = 10 + 20;
Variable Expression - If any of the operands is a variable, it becomes a variable expression. It is evaluated at runtime (when we run the compiled code).
e.g.
a + b
a * 10 + b
int a = b;
Datatypes are used to store different types of data into memory. There are eight primitive datatypes within Java. I’ve clubbed them into four parts. Let’s discuss them one by one.
Integer Datatype - There are four datatypes that can be used to store integer values. All of them are signed. Unlike C/C++, in Java we don’t have unsigned datatypes.
Datatype | Value Range | Size | Default Value |
---|---|---|---|
byte | -128 to 127 | 1 byte | 0 |
short | -32768 to 32767 | 2 bytes | 0 |
int | -2147483648 to 2147483647 | 4 bytes | 0 |
long | -2^63 to 2^63-1 | 8 bytes | 0L |
Floating Point Datatypes - A floating point variable can represent a very wide range of values but with a fixed number of digits of accuracy.
Datatype | Value Range | Size | Default Value |
---|---|---|---|
float | ± 3.4E38 | 4 bytes | 0.0f |
double | ± 1.7E208 | 8 bytes | 0.0d |
Floating point literals are treated as double by default in Java so we need to use suffix F/f which means
float a = 1.2;
will give error, whereas double a = 1.2;
or float a = 1.2f;
is valid.
Character Datatype - It takes the size of two bytes and a range from 0 to 2^16-1. A char
type variable can be used to hold a single character.
e.g. char ch = 'A';
Alternatively, we can also provide ASCII values.
e.g. char ch = 65;
65 is ASCII value of A.
Boolean Datatype - A boolean
type variable can be used to hold logical value either true
or false
.
e.g. boolean pass = true;
Widening of a datatype is done when we assign a smaller type to larger type (i.e. short to int).
Example -
1 2 3
int a = 25; double b = 3.14; b = a;
Narrowing, on the other hand, is opposite, where we assign larger datatypes to smaller datatypes (i.e double to float). By default, this is not allowed by Java language and leads to a compile time error. But, by doing explicit typecasting we can assign larger types to smaller ones.
Example -
1
2
3
4
int a = 25;
double b = 3.14;
a = b; // leads to error
a = (int) b; // ok
Arithmetic Operators - Java supports many arithmetic operators
1
+, -, *, /,%,++,--
for all numeric datatypes. ++
is an increment operator which increases the value by one. --
is a decrement operator which decreases the value by one.
Shortcut Operators - +=, -=, *=, /=, %=
They are used to reduce the length of expression.
e.g. a = a + 2 or a += 2 both are the same.
Bitwise Operators - These operators are used to manipulate individual bits of a variable. Some of them are as follows -
Bitwise left shift operator: This operator is used to shift individual bits of a variable toward the left by a specified number of positions and it fills the vacant position with 0.
a = a << n;
or a = a * 2^n
Bits operations are faster than arithmetic.
e.g.
1 2
int a = 2; a = a << 4;
Output : 32
AND(&), OR(|), XOR(^), NOT(~)
are some other bitwise operators. For more details check out this.
Now we have learned about some aspects of Java language. Let’s start by installing Java into our machine and running our first program.
Open any text editor (i.e. Sublime Text, VSCode, Atom, Eclipse, IntelliJ IDEA) and write the below code -
1
2
3
4
5
6
7
class Demo {
public static void main(String args[]) {
int x = 1, y = 2, sum;
sum = x + y;
System.out.println("Total is " + sum);
}
}
Let’s go through each aspect of this code. First we begin with defining a class. Then we define a main method.
void - specifies that the method doesn’t return any result to the calling environment.
public - specifies that the method is accessible from outside the class.
static - specifies that the method can be called without creating an object of the class.
String args[] - while running a program at the command prompt we can pass some arguments to it. This array of Strings is used to hold the arguments provided at the command prompt.
Now save this file as Demo.java
Go to terminal and run this command javac Demo.java
to compile the code. It’ll create a class file. Now after this run java Demo
command and it’ll show the output in the terminal.
That’s it! You have run your first Java program. Now it’s your turn to utilize all that you have learned so far. We’ll meet in the next part. Til then happy coding :)