0% found this document useful (0 votes)
2 views6 pages

Basic Java

The document provides an overview of basic Java concepts including file inspection, compiling, and running Java programs. It covers essential topics such as variable types, data types, operators, and decision-making statements, along with rules and conventions for naming and using identifiers. Additionally, it explains the Java Unicode system and the importance of comments in code documentation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Basic Java

The document provides an overview of basic Java concepts including file inspection, compiling, and running Java programs. It covers essential topics such as variable types, data types, operators, and decision-making statements, along with rules and conventions for naming and using identifiers. Additionally, it explains the Java Unicode system and the importance of comments in code documentation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Java

Saturday, 18 July 2026 3:51 AM

Part 0:
Inspect java files:

Version:

Compile & Run:

Compile 2 files simultaneously:

Check:

[Link]: Hexadecimal form

Part 01:

Qn: Java File name must be the same as the class name. Why?

The main Method:


It is the starting point of the program.

# [Link] represents the primary console and its println() method is taking "Hello World" as input and it prints
the same to the console output.

Comments
In Java, there are three types of comments:
• Single-line comments
• Multiline comments
• Documentation comments: can be understand by Javadoc tool & can be used to create HTML-based
documentation.
#Rules & Conventions:
1. Case Sensitive.
2. Class name: Upper start. i.e.
a. Main
b. MyFirstClass
3. Method naming: Camel case.
a. sum
b. sumOfDigits
4. Program file name: Same as class name. If no public class available in file, this rule don't apply.
5. Every statement must end with semicolon.

Academic Page 1
Identifiers: Names of variables, methods, classes.
#Rules:
1. Must begin with (A - Z), (a - z), _, $
2. Starting with numbers is prohibited. But can be used elsewhere.
3. Case sensitive, no keywords

Variables:
Named storages/ containers that programs can manipulate.
Must mention data type, which determines the memory size & type of the data.

3 Kinds:

1. Local: Created, visible & destroyed inside methods, constructors or blocks.


Access modifiers cannot be used for local variables. Implemented at stack level. No default values, must be
initialized while declared.

2. Class/ Static:
a. Declared with static keyword
b. Inside class but not in constructor, method or block
c. Belongs to the class, not to the instance. Means, there is only one copy of the variable. All objects of
that class type share the same value.
d. Is created when the program starts and destroyed when it ends.
e. Stored in static memory
f. Often used as constants (public static final)
g. Called using class name. i.e. x = [Link];
h. Have default values. Same as instance variables.
Numbers -> 0
Boolean -> false
Objects -> null

1. Instance/ non-static:
a. states/ properties of instances/ objects. Is created when an object is created & destroyed when the
object does.
b. Declared inside Class but not inside method/ constructor/ block.
c. When a space is allocated for an object in the heap, a slot for each instance variable value is created.
d. Access modifiers are used with. Advised to declare as "private", gradually update visibility when
required.
e. Is accessible by all methods, constructor & blocks of the class.
f. Have default values.

g. Values can be assigned while declared/ via methods, constructors too.

Modifiers:

Array: an object on the heap


Enums: restrict a variable to have only a few predefined values (called enums)

Here, size can only be SMALL, MEDIUM or LARGE.


Note −
Enums can be declared --> own/ inside a class
Methods, variables, constructors --> on enums

Data Type:
Define the type of data & their range.
Used to declare variables, in return type & parameters etc.
Tells the compiler what type of data is stored, their range, & required memory.
Based on the type OS allocate memory

2 types:
Primitive

Academic Page 2
1. Primitive
2. Reference/ Object
Primitive: Predefined in language & named by keywords. 8 of such kind.

Primitive data type size formula:


In 2's complement system, for a n bit signed integer,
Total values we can present 2 ^ n.
Negative values = 2 ^ n / 2 = 2 ^ (n - 1)
Non negative values = total 2 ^ (n - 1) numbers.
= 0 + positive
= till 2 ^ (n - 1) - 1.

Notes
• Default values only applied to instance & class variables. Local variables must be initialized with
values.
• Integer types (byte, short, int, long) use 2's complement representation.
• float and double follow the IEEE 754 floating-point standard.
• char stores a UTF-16 code unit, allowing values from 0 to 65,535.

Name size range(show formula & numarical range ( actual + 10 ^ x appromission) default values use cases
important info

Non-Primitive: Not predefined. These types are created using constructors of the classes.
Their default values are null.

Type Casting/ Conversion:


Convert one data type to another
Either by compiler or by programmer
2 types of casting is allowed
1.
• Widening/ Implicit type casting: small sized type -> large sized type. Compiler does it automatically. Change the
type at compile time.

• Narrowing/ Explicit type casting: large -> small. Done by programmer. Explicit type casting converts a value at
runtime. It does not change the declared type of a variable.

Variable types in Java are fixed at compile time and cannot change during runtime.

Academic Page 3
Variable types in Java are fixed at compile time and cannot change during runtime.

Java Unicode System:


Unicode is an international character set that encompasses a vast range of characters, symbols, and scripts from
many languages across the globe. Java programming language, being platform -independent, has built-in support
for Unicode characters, allowing developers to create applications that can work seamlessly with diverse
languages and scripts.
Before Unicode, there were multiple standards to represent character encoding −
• ASCII − for the United States.

• ISO 8859-1 − for Western European Language.

• KOI-8 − for Russian.

• GB18030 and BIG-5 − for Chinese.

So to support multinational application codes, some character was using single byte, some two. An even same
code may represent a different character in one language and may represent other characters in another
language.
To overcome above shortcoming, the Unicode system was developed where each character is represented by 2
bytes. As Java was developed for multilingual languages it adopted the Unicode system. The lowest value is
represented by \u0000 and the highest value is represented by \uFFFF.

Working approaches with java characters:


1. With Escape Sequences:
When the character can't directly be typed or displayed in java code.
starts with the characters '\u' followed by four hexadecimal digits that represent the Unicode code point of
the desired character. i.e.

2. Direct typing: First choice


Wrapped in single quotes

Input: Using built-in Scanner class objects & its methods.

Operators: perform operations on variables and values


○ Arithmetic Operators
○ Assignment Operators
○ Relational Operators
○ Logical Operators
○ Bitwise Operators
○ Miscellaneous Operators

Unary arithmetic operators: ++, --, +, - (negation)


These operators work with numeric data types like int, float, double, and long.

post increment/ decrement: execute -> update


pre increment/ decrement: update -> execute

Bitwise operator:
Perform bit by bit operations
Operators: AND, OR, NOT/ 1's complement, XOR, left shift, right shift

Academic Page 4
Miscellaneous Operators:
1. Ternary Operator:

2. Instanceof Operator:
Check if an object is an instance of a particular Class/ Interface.
Return either True or False.

Operator precedence & associativity:


Precedence: priority in multi operator expression
Associativity: priority if precedence is equal.
i.e.
10 + 5 * 2 - 8 / 4
=10 + 10 - 2 [*, / have same precedence, L -> R associativity]
=20 - 2
=18

a = b = c = 40
Here, = has R->L associativity
c=40
b=c=40
a=b=c=40

Tips: Use () to manipulate precedence & associativity.

Part 02:
Statement vs Expression:

Decision Making Statements:


1. If - else statements

If one condition hits none of the remaining statements will be executed.


No other codes are allowed in between if….else if….else
Curly Braces for Multiple Statements
Single Statement Optional Braces
Else is Optional

Note: The conditional operator is not a statement, it's an operator. Can be used instead of if..else

2. Switch statements

When we need to use multiple if-else for equality test


Variable/ expression result must be:
a. integers (int, long, byte, short)
b. Same across case values (by both value & type)
When a case is hit, the following expressions will be executed untill a break hit.
If no break with any case, the next cases will also get compared.
Default & break are optional

Academic Page 5
Academic Page 6

You might also like