0% found this document useful (0 votes)
57 views

Comp Notes

Java fundamentals provides an overview of key Java concepts including: - Java uses the Unicode character set and resembles C/C++. - Comments help document code using /* */, //, and /** */ styles. - The smallest units in a program are tokens like keywords, identifiers, literals, and operators. - Java has 8 primitive data types including int, double, char, and boolean. Reference types include arrays and strings. - Variables store values and constants cannot change once assigned using final. - Operators perform functions on data and have precedence like arithmetic before relational. - The Scanner class reads user input using methods like nextInt() and nextLine(). - Expressions can be arithmetic

Uploaded by

ananya k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Comp Notes

Java fundamentals provides an overview of key Java concepts including: - Java uses the Unicode character set and resembles C/C++. - Comments help document code using /* */, //, and /** */ styles. - The smallest units in a program are tokens like keywords, identifiers, literals, and operators. - Java has 8 primitive data types including int, double, char, and boolean. Reference types include arrays and strings. - Variables store values and constants cannot change once assigned using final. - Operators perform functions on data and have precedence like arithmetic before relational. - The Scanner class reads user input using methods like nextInt() and nextLine(). - Expressions can be arithmetic

Uploaded by

ananya k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JAVA FUNDAMENTALS

- Java has been designed to resemble some of the most popular programming languages
like C & C++

JAVA CHARACTER SET


- Character set is a set of valid characters that a language can recognize. A character
represents any letter, digit or any other sign.
- Java uses the Unicode character set.
- Unicode is a character coding scheme that has character codes for numbers, arithmetic
symbols, special characters and for the letters in almost all the languages in the world

COMMENTS
Comments are useful in documenting a java program. They provide valuable information to a
programmer and make it easier for the programmer to understand the program.

- /* */ style comments can comment out multiple lines so they are useful when you want to
remove large blocks of code, perhaps for debugging purposes.
- // style comments are better for short notes of no more than a line.
- /** */ style comments are used for documentation purposes. These type of comments
stand out clearly and are easier to read.
- Note: /* */ can be used in the middle of a line whereas // can only be used at the end.
However putting a comment in the middle of a line makes code harder to read and is
generally considered to be bad.

TOKENS
The smallest individual unit in a program is known as a token.
Keywords
Identifiers
Literals
Operators
Separators

KEYWORDS
A keyword are words that conveys a special meaning to the compiler.
i.e. reserved words
IDENTIFIERS
Identifiers are names given to different parts of the program.

- Can have alphabets, digits, underscores and dollar sign.


- Must not be a keyword, Boolean literal (like true and false or null literal(like null)).
- Must not begin with a digit.
- Java is case sensitive i.e., upper-case letters and lower-case letters are treated differently.
- Must not have spaces

LITERALS
Literals are data items that have fixed data values.
- Integer literals, Float literals, Boolean literals, Character literals, String literals, Null
literals

OPERATORS
Operators are tokens that perform specific functions on data.
- Arithmetic, Relational, Logical

SEPARATORS
Used as separators or punctuators.
() {} [] ; , .
DATA TYPES, VARIABLES, LITERALS AND CONSTANTS IN JAVA

DATA TYPES
- Primitive or In-built data type
- Reference Data Type

PRIMITIVE DATA TYPE


Also known as fundamental data type are the in-built data types in Java. There are 8 primitive
data types:
- Integral
byte, int, char, long
- Float
float, double

- Character
char

- Boolean
boolean

type size
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
boolean reserves 8 bits, uses only 1 bit

REFERENCE DATA TYPES


In built reference types:
- Arrays
- Strings

User-defined reference types (also known as user-defined objects or user-defined data types)

VARIABLES

A variable is a named storage location in the computer’s memory.

- It can hold only one value of a particular data type.


- The value in a variable can be manipulated during the execution of the program.

CONSTANTS
A constant is a variable whose value cannot be changed once it has been assigned a value.
If the value is changed subsequently in the program, the compiler gives an error.
The keyword final makes a variable as constant.
example: final double pi=3.14

Advantages of constants:
- Indicates to the programmer that the value in that constant (variable) cannot be changed
and hence makes it easier to read and check for correctness. Even if the programmer
accidentally changes the value of that constant in the code, the compiler gives an error
- If the value in the constant needs to be changed, only the declaration needs to be
modified. On the other hand if a literal is used, one has to search through the entire
program for every occurrence of that specific value and then change all the occurrences
SUFFIXES
Suffixes are used to explicitly specify the data type of a literal data value.

- In some situations it is required, because the compiler gives an error, when the value that
is assigned to a variable is beyond the capacity of the variable on the left hand side.
- Suffixes are f or F , d or D, l or L.

OPERATORS

Operators can be classified in two ways. It can be based on the :


- number of operands they use
- type of operations they perform

NUMBER OF OPERANDS THEY USE


- Unary (+, -, ++, --, !)
- Binary (+, -, *, /, %, <=, , >=, !=, &&, ||, =, == +=, -=, *=, /=, %=)
- Ternary (?:)

TYPE OF OPERATIONS THEY PERFORM


- Arithmetic (+ - / * %)
- Assignment (= += -= *= %= /=)
- Prefix/Postfix operators (++ --)
- Relational (> < <= >= == !=)
- Logical (&& ||)
- Conditional (?:)

PRECEDENCE

BODMAS and Associativity Rules

1. Expression with Parenthesis


2. Prefix Operators (Increment ++, Decrement --)
3. Arithmetic operators
4. Relational operators
5. Logical Operators
6. Assignment Operators (right to left)

SCANNER CLASS

The Scanner class is defined within the Java Package “util”


Syntax:
import java.util.*;

Scanner sc=new Scanner(System.in);

next().charAt(0) : reads a character


next() : reads a String without spaces
nextLine() : reads a String that can have spaces
nextInt() : reads data of int type
nextByte () : reads data of byte type
nextShort() : reads data of short type
nextLong() : reads data of long type
nextFloat () : reads data of float type
nextDouble() : reads data of double type
nextBoolean() : reads data of boolean type

Scanner Syntax
Scanner in = new Scanner(System.in); // creating a Scanner object
<variable data type>=<scanner object>.next(<data type>);
ARITHMETIC AND BOOLEAN EXPRESSIONS

EXPRESSIONS
An expression is composed of one or more operations.

Types of expressions:
- Arithmetic expression
- Boolean/Logical expression

ARITHMETIC EXPRESSIONS

An expression that results into a numeric value (Integral/Real) is an Arithmetic expression.


Arithmetic expressions may be a combination of constants, variables, literals, Arithmetic
operators and Java’s Mathematical functions.
In an expression, two or more variables and operators of the same type should not occur in
continuation .

Types of arithmetic expressions


- Pure Expressions
All operands of same data type
- Mixed Expressions
The operands are of different data types

BOOLEAN EXPRESSION / LOGICAL EXPRESSION

An expression that results into false or true is called a Boolean expression or a Logical
expression
- A Boolean expression that is formed only with variables, constants, literals and relational
operators is called a Simple Relational or Logical expression.
- A Boolean expression that is formed with more than one simple relational expression,
linked by logical operators is called a Complex Relational or Logical Expression

TYPES OF ERRORS

Compilation Error
- Semantic error
- Syntax error
Logical Error
Runtime Error

SEMANTIC ERROR
Caught by compiler at the time of compilation
- When you use prefix operators with Boolean variable
- Declaring a variable twice using the same data type/different data type
- Type incompatibility
- Using a non-initialized variable

SYNTAX ERROR
Caught by compiler at the time of compilation
- Semicolon is missing
- The keyword is misspelt or in upper case
- Curly braces or parenthesis are missing
- Identifiers are not valid
- Variables are not declared
- When 2 operands and/or literals occur continuously without an operator in between them.

LOGICAL ERROR
The programmer has to trace this type of error.
When the program does not give the expected output, then it is said to have a logical error.
Can be the hardest errors to find. You need to spend time going through your code looking for a
precise reason for the error.

RUNTIME ERROR
This occurs during the execution of the program and it causes the program to end abruptly.
The programmer has to anticipate these types of errors while writing the program.
Occurs when
◦ a numeric literal or variable is divided by zero
◦ when the user inputs a string or a character, where a number is expected.
CONDITIONAL CONSTRUCT

IF CONSTRUCT

An “if” statement consists of a Logical Expression. If the condition evaluates to true the
statement to be executed is placed after the “if” statement. The statement after the else part is
executed if the condition is not true.

Syntax:
if(logical expression)
statement;
else
statement;

IF ELSE IF CONSTRUCT
Multiple conditions which are related can be specified with the “else if” clause.
When there are N number of possibilities, then there needs to be N-1”if” and “else if” statements.

if(logical expression)
statement;
else if(logical expression)
statement;
else
statement;

NESTED IF CONSTRUCT

A “nested if” is basically another set of if statements, inside another “IF” construct with or
without the else clause.
“Nested if” statements are useful when we need to execute specific set of statements for each
conditional construct.

if(num>0) // outer if
{
if(num<10) //inner if
System.out.println(num+” is between 0 and 10”)
TYPE CONVERSIONS

the process of converting one predefined data type into another is called type conversion.

2 types of type conversions:


- implicit type conversion (aka promotion or coercion)
- explicit type conversion (aka type casting)

IMPLICIT TYPE CONVERSION

this type of conversion is performed in Java without the programmer’s intervention. it is


generally applied in a mixed arithmetic expression.

this type of conversion usually is applied in a mixed arithmetic expression. all operands are
converted to the largest data type in the expression, to prevent loss of data.

a data type of lower size (occupying less memory) is assigned to a data type of higher size. this is
done implicitly.

example:
int x=10
double y=x

EXPLICIT TYPE CONVERSION

this is a user defined conversion that forced an expression to be of specific type. this is called
type casting.

it can be done for a variable or literal or an expression

potential conversion problems:


- conversion from float to integer may result in the loss of fractional part.
- in case of conversion from bigger integer to smaller integer type, original value may be
out of range for target type, resulting in loss of information.
- in the case of conversion from bigger floating-point type to smaller floating-point type,
there will be a loss of precision (significant figures) and original value may be out of
range for target type, thereby resulting in loss of information.

impossible conversions:
- primitive type to a reference type and vice versa
- null value to a primitive type
- primitive to Boolean
- Boolean to primitive type
SWITCH CASE

A “switch… case” is a more compact statement and a faster alternative to the “if” construct.
It is generally used when a value must be tested against a list of values for equality.

it cannot be used in situation where a range of values or more complex conditions need to be
checked.

it is a multiple branch selection statement that allows a variable or the result of an arithmetic
expression to be tested for equality against a list of values.
FALL THROUGH

whenever there is no break statement involved in a switch case block, all the statements are
executed even if the test expression is satisfied.

PROGRAMMING CONSTRUCTS

programming constructs can be defined as types of statements in a programming language.


FOR LOOP

A for loop is a repetition control structure that allows you to repeatedly execute one or more
statements a specific number of times.

syntax:
for(initialization; boolean_expression; update_expression)
{
statement;
statement;
}

It is also known as an entry-controlled loop, as the body of the loop is only executed if the test
expression is true.
multiple initializations and update expressions are separated by commas.

infinite loop:
- it is a loop that runs indefinitely. it normally occurs if the test condition is incorrect. it
results in a logical error.
empty loop:
- this loop does not contain any statement in its loop-body, thus becomes an empty loop.
- semicolon at the end of the for loop indicates that it is a for loop with no statement under
it.
- empty loops can be used as timer loops and for making the code more compact.
- timer loops can be used to cause a delay or create a pause in the program.

WHILE LOOP

A while loop continually executes a block of statements while a particular condition is true.

In a while loop, a loop control variable should be initialized before the loop begins as an
uninitialized variable cannot be used in the expression in the while statement.
The loop variable should be updated inside the body of the while loop.

syntax:
initialization statement;
while(boolean expression)
{
statement(s);
update statement;
}
Similar to the ‘for’ loop the while loop is also an entry-controlled loop as the body of the loop
might never be executes, when the expression is tested and the results is false.
the actions inside the loop will be executed till the result of the Boolean expression remains true.

In a for loop construct, initialization, conditional expression and update statement can be in the
loop statement, whereas in a ‘while’ loop construct, only the conditional expression can be in the
loop statement.

DO WHILE LOOP

A do while loop is like a while loop, except that a do while loop is guaranteed to execute at least
once.

syntax:
do
{
//statements
}
while(boolean_expression);

this is called an exit controlled loop because the condition is tested after the statements are
executed at least once.
TERNARY/CONDITIONAL OPERATOR

The ternary/conditional operator is the only operator to take three operands.

It is an operator that is used for evaluating a conditional expression and executing an operation
based on the result of the condition.

syntax:
boolean expression ? value 1 : value 2

it has a lower precedence as compared to other operators


PREFIX AND POSTFIX OPERATORS

++, -- are the Postfix Operators They are Unary operators.

The increment operator ++ or the decrement operator -- can be put in front of a variable or
behind a variable.

When it is put before, it is a prefix operator. When it is put behind a variable it is a postfix
operator.

In both the cases, the operators either increment or decrement the value of a variable. However,
the operators work differently when they are used as part of an arithmetic expression.

The prefix operator has the highest precedence. The value of the variable it precedes is first
increased/decreased by 1 and then it used in a binary operation.
JUMP STATEMENTS

BREAK STATEMENT

The ’break’ statement in Java is used in the “switch..case” construct and in the Looping
constructs.
When the break statement is encountered inside a loop, the loop is terminated immediately (even
if the test expression of the loop is still true), and the program control resumes at the next
statement following the loop. It is used with the ‘if’ construct.

The break statement must be the last statement inside the ‘if’ construct.

CONTINUE STATEMENT

The “continue” statement continues with the next iteration skipping the rest of the loop
statements in the loop body.

In the case of for loop, “continue” transfers the control to the update statement in the for loop
and then evaluates the test-expression.
In the case of “do while” and “while” loop, the program control passes to the test-expression.
The next iteration is executed only if the test expression evaluates to true.

NESTED LOOPS

When a loop contains another loop in its body, then it is said to be a Nested Loop.
In a nested loop, the outer loop determines the number of times the inner loop runs.

If a “break” statement appears in a nestedloop, then it causes an exit from the very loop it
appears in. In other words it exits out of the inner loop only. In order to exit out of the outer loop,
another break statement needs to be given.
Similarly the “continue” statement, continues with the iteration in the loop it appears in. For
example, if it is in the inner loop, then it continues with the iteration in the inner loop.

DEFINTIONS

Character set is a set of valid characters that a language can recognize.

The smallest individual unit in a program is known as a token.

A keyword are words that conveys a special meaning to the compiler.

Identifiers are names given to different parts of the program.

Literals are data items that have fixed data values.

Operators are tokens that perform specific functions on data.

A variable is a named storage location in the computer’s memory.


A constant is a variable whose value cannot be changed once it has been assigned a value. If the
value is changed subsequently in the program, the compiler gives an error.

An expression is composed of one or more operations.

An expression that results into a numeric value (Integral/Real) is an Arithmetic expression.

An expression that results into false or true is called a Boolean expression or a Logical
expression

An “if” statement consists of a Logical Expression. If the condition evaluates to true the
statement to be executed is placed after the “if” statement. The statement after the else part is
executed if the condition is not true.

A “nested if” is basically another set of if statements, inside another “IF” construct with or
without the else clause.

Pure Expressions are where all operands of same data type

Mixed Expressions are where the operands are of different data types

the process of converting one predefined data type into another is called type conversion.

a data type of lower size (occupying less memory) is assigned to a data type of higher size. this is
done implicitly and is known as implicit type conversion.

this is a user defined conversion that forced an expression to be of specific type. this is called
type casting.

A “switch… case” is a more compact statement and a faster alternative to the “if” construct.

programming constructs can be defined as types of statements in a programming language.

A for loop is a repetition control structure that allows you to repeatedly execute one or more
statements a specific number of times.

infinite loop is a loop that runs indefinitely. it normally occurs if the test condition is incorrect. it
results in a logical error.

If a loop does not contain any statement in its loop-body, it is said to be an Empty loop.

A while loop continually executes a block of statements while a particular condition is true.

A do while loop is like a while loop, except that a do while loop is guaranteed to execute at least
once.
The ternary/conditional operator is the only operator to take three operands.

You might also like