Basic
Basic
Basics
List of Concepts Involved:
Java Outpu
Java Variable
Java Identifiers
Java Data Type
Java Operator
Java operators precedence
A ‘Hello World in Java’ programme is a simple programme that displays the ‘Hello World in Java’ on the screen.
Let us take a look at how the Java ‘Hello World Java’ programme works.
// First Program
class HelloWorldJava {
Output
Hello World Programme in Java
// First Program
Any line in Java that begins with // is a comment. Comments are intended to help users reading the code
understand the programme's intent and functionality. The Java compiler completely disregards it.
class HelloWorldJava
Every Java application starts with a class definition. The class in the programme is called HelloWorldJava, and
its definition is as follows:
Java + DSA
class HelloWorldJava {
…..
We have to keep in mind that every Java application has a class definition, and the class name should match
the name of the file in Java.
a. Public: This is a visibility specifier or access specifier that defines the component's visibility. The term ‘public’
refers to a parameter or component that is visible to everyone.
b. Static: The keyword ‘static’ indicates that the method/ object/ variable that follows this keyword is static and
can be invoked without the object or the dot (.) operator. The presence of the static keyword before the main
method indicates that the main method is static.
c. Void: The keyword ‘void’ indicates that the method returns nothing.
d. Main: The keyword ‘main’ denotes the main method, which is the starting point for any Java programme. A
Java programme's execution begins with the main method. The curly braces {} indicate start and end of main.
e. String[] args: The command line arguments are stored in the string array args.
System.out.println (IMPORTANT)
System. out. The println function is used to print messages to the screen. In
Java, the system is a class. The PrintStream class is represented by the parameters "Out" and "println." Println is
a method, whereas "out" is an object.
The built-in method print() is used to display the string which is passed to it.
This output string is not followed by a newline, i.e. the next output will start on the same line.The built-in method
println() is similar to print(), except that println() outputs a newline after each call.
Example Code:
System.out.println("Hello World");
Java + DSA
Output:
Hello World
The variable_name is the name of a variable. We can initialize the variable by specifying an equal sign and a
value (Initialization is optional).However, the compiler never assigns a default value to an uninitialized local
variable in Java.
Java + DSA
Here, rate is an int data type variable with the value 40 assigned to it.
The variable can only hold integer values, as indicated by the int data type.
In the example, we assigned a value to the variable during the declaration process. However, it is not required.
int rate;
rate = 40;
System.out.println(rate); // 50
rate = 60;
System.out.println(rate); // 60
A Java keyword (reserved word) cannot be used as a variable name. For example int float; //invalid
expression as float is defined as a keyword in java
Here, we should use variable names with more than one word with all lowercase letters for the first word and
capitalization of the first letter of each subsequent word. Take, for example, cricketScore. This is called
camelcase
When creating variables, It's preferable to give them meaningful names-age,earning,value for example,
make more sense than variable names like a, e, and v
Use all lowercase letters when creating one-word variable names. It's preferable to use physics rather than
PHYSICS or pHYSICS, for example.
Java + DSA
Topic 3: Java Identifiers
Java identifier is a name given to a package, class, interface, method, or variable. All Java components require
names. Names used for classes, variables, and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows -
Rule 1 − All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_)
Rule 2 − After the first character, identifiers can have any combination of characters
Rule 3 − A keyword cannot be used as an identifier
Rule 4 − The identifiers are case sensitive
Rule 5 – Whitespace is not permitted
Examples of legal identifiers: rank, $name, _rate, __2_mark
Examples of illegal identifiers: 102pqr, -name.
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
N on primitive data types: The non-primitive data types include lasses, trings,Interfaces, and Arrays.
- C S
Boolean flag=true;
B yte Type
Values for the byte data type range from -128 to 127 (8-bit signed two's complement integer)
To save memory, it is used instead of int when it is certain that the value of a variable will be between -128
and 127.
Java + DSA
For example,
byte range=105;
Short Type
In Java, the short data type can have values ranging from -32768 to 32767 (16-bit signed two's complement
integer)
If the value of a variable is certain to be between -32768 and 32767, it is used instead of other integer data
types (int, long).
For example,
short loss=-50;
Int Type
Values for the int data type range from -2^31 to 2^31-1 (32-bit signed two's complement integer)
You can use an unsigned 32-bit integer if you're using Java 8 or later. This will have a value of 0 as the
minimum and a value of 232-1 as the maximum. For more information, see ‘How to Use an Unsigned Integer
in Java 8?’.
For example,
int profit=5000;
Long Type
Values for the long data type range from -2^63 to 2^63-1 (64-bit signed two's complement integer)
You can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264-1 if you're
using Java 8 or later.
For example,
long profit=455559990;
Double Type
The double data type is a 64-bit floating-point data type with double precision
It should never be used for exact values like currency.
For example,
double height=12.5;
Java + DSA
Float Type
The float data type is a 32-bit single-precision floating-point value. If you're curious, you can learn more
about single-precision and double-precision floating-point
It should never be used for precise values like money.
For example,
float depth=-32.3f;
Char Type
It's a Unicode 16-bit character
The char data type has a minimum value of 'u0000' (0) and a maximum value of 'uffff'.
For example,
char temp=’a’;
MCQs
Compiler assigns a default value to an uninitialized local variable in Java Programming.
a) True
b) False
Ans b) false
Explanation:
In java, it's compulsory to initialize any local variable before using it because compilers don't assign any default
value to variables.
Which of the following data type can store the longest decimal number ?
Options:
a) boolean
b) double
c) float
d) long
Ans b) double
Java + DSA
Explanation:
Out of all given options, only float and double can hold decimal numbers and double’ is the longest data type
with 64-bit defined by Java to store floating-point values.
a) Special symbols
b) Letter
c) String
d) Digit
Ans c) String
Explanation:
String is a collection of characters and is stored in a variable of String data type.
Assume integer variable num1 holds 20 and variable num2 holds 30, then:
+ Addition
2w g
Example: num1 + num ill ive 5 0
g f f
Subtracts ri ht hand operand rom le t hand operan d
- Subtraction
2w g
Example: num1 - num ill ive -1 0
Java + DSA
Multiplies values on either side of the operator
* Multiplication
Example: num1 * num2 will give 600
/ Division
Example: num1 / num2 will give 0.67
% Modulus
Example: num2 % num1 will give 10
++ Increment
Example: num2++ gives 31
-- Decrement
Example: num1-- gives 19
Example:
class Main {
int result;
// addition operator
result=p+q;
System.out.println(result);
// subtraction operator
System.out.println(p - q);
// multiplication operator
System.out.println(p * q);
// division operator
System.out.println(p / q);
// modulo operator
System.out.println(p % q);
Java + DSA
Output
30
10
200
class Main {
// create variables
// == operator
// != operator
// > operator
// < operator
// >= operator
// <= operator
Java + DSA
Output
false
true
false
true
false
true
&& (Logical AND) expression1 && expression2 true only if both expression1 and
expression2 are true
Example code:
class Main {
// && operator
int p=15,q=10,r=5;
// || operator
// ! operator
Java + DSA
Output
true
false
true
true
false
true
false
= p = q; p = q;
+= p += q; p = p + q;
-= p -= q; p = p - q;
*= p *= q; p = p * q;
/= p /= q; p = p / q;
%= p %= q; p = p % q;
class Main {
int p = 10;
int q;
Java + DSA
// assign value using =
q = p;
System.out.println(q); // value of q is 10
q += p;
System.out.println(q); // value of q is 20
q *= p;
Output
10
20
200
Operator Meaning
Java + DSA
For example:
class Main {
// initialize p
int p = 5;
System.out.println("Post-Increment Operator");
System.out.println(p++); // 5
// initialized to 0
int q = 5;
System.out.println("Pre-Increment Operator");
System.out.println(++q); //6
// returned
Output:
Post-Increment Operator
Pre-Increment Operator
Operator Description
~ Bitwise Complement
^ Bitwise exclusive OR
Java + DSA
Example code:
class Main {
// initialize p
int p = 5;
System.out.println(p<<2);
Output: 20
Explanation:
Shifting the value of p towards the left two positions will make the leftmost 2 bits to be lost. The value of p is 5.
The binary representation of 5 is 00000101.
After 2 left shifts , binary representation of p will be 00010100 which is equivalent to 20.
int ans = 10 - 4 * 2;
What will be the value of variable ans? Will it be (10 - 4)*2, that is, 12? Or it will be 10 - (4 * 2), that is, 2?
When two operators share a common operand, 4 in this case, the operator with the highest precedence is
operated first.
In Java, the precedence of * is higher than that of -. Hence, the multiplication is performed before subtraction,
and the value of variable ans will be 2.
Associativity specifies the order in which operators are executed, which can be left to right or right to left. For
example, in the phrase p = q = r = 10, the assignment operator is used from right to left. It means that the value
10 is assigned to r, then r is assigned to q, and at last q is assigned to p. This phrase can be parenthesized as (p
= (q = (r = 10)).
Java + DSA
Operator Precedence in Java (Highest to Lowest)
P
Unary ++ - -+ - ! ~ Right to left
r
Multiplicative */% Left to right
e
e
Shift << >> >>> Left to right
d
Relational < <= > >= Left to right
e
Equality == != Left to right
n
o
Bitwise OR | Left to right
r
Logical AND && Left to right
d
Logical OR || Left to right
e
Example 1.
Let say we have 4 variables of type int ; p,q,r,s
s = p-++r-++q;
is equivalent to
Answer: s = p-(++r)-(++q);
Explanation: The operator precedence of prefix ++ is higher than that of - subtraction operator.
Example 2. What does the following code fragment print?
System.out.println(4 + 2 + "pqr");
System.out.println("pqr" + 4 + 2);
Java + DSA
Answer: 6pqr and pqr42, respectively.
Explanation: The + operator is left-to-right associative, whether it is string concatenation or addition.
boolean p = false;
boolean q = false;
boolean r = true;
System.out.println(p == q == r);
Java + DSA