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

Introduction to Java

The document provides a comprehensive overview of Java, a platform-independent programming language developed by Sun Microsystems, covering its character set, keywords, data types, operators, and error handling. It explains fundamental concepts such as classes, objects, methods, and variable management, including scope and type conversion. Additionally, it discusses various operators, expressions, statements, and the importance of comments in coding.

Uploaded by

nzlkharel
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)
3 views

Introduction to Java

The document provides a comprehensive overview of Java, a platform-independent programming language developed by Sun Microsystems, covering its character set, keywords, data types, operators, and error handling. It explains fundamental concepts such as classes, objects, methods, and variable management, including scope and type conversion. Additionally, it discusses various operators, expressions, statements, and the importance of comments in coding.

Uploaded by

nzlkharel
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
You are on page 1/ 8

• Introduction to java: Java is a general-purpose programming language that is designed to be

platform-independent, meaning that it can run on a wide range of devices and operating
systems. It was developed by Sun Microsystems in the 1990s and is now owned by Oracle. Java
is widely used for developing desktop, mobile, and web applications, as well as in the Internet of
Things (IoT) and big data.

• Character set: The character set used by Java includes all of the standard ASCII characters, as
well as a wide range of Unicode characters. This allows Java programs to handle text in multiple
languages and scripts.

• Keywords: Keywords are reserved words in Java that have special meaning in the language and
cannot be used as identifiers. Examples include "public", "class", "void", "if", "else", "for",
"while" and "final".

• Identifiers: Identifiers are names given to variables, methods, classes, and other elements in a
Java program. They must start with a letter, underscore, or dollar sign and can be followed by
letters, digits, underscores, or dollar signs. Identifiers are case-sensitive, meaning that
"myVariable" and "myvariable" are considered to be different identifiers.

• Literals: Literals are fixed values that can be assigned to variables in a Java program. Examples
include numbers (e.g. 5, 3.14), characters (e.g. 'a', 'z'), and strings (e.g. "Hello World").

• Operators: Operators are special symbols that perform specific operations on variables, such as
mathematical operations or comparisons. Examples include + (addition), - (subtraction), *
(multiplication), and / (division).

• Separators: Separators are characters that separate elements of a Java program. Examples
include ; (semicolon), , (comma), and {} (curly braces).

• Constants: Constants are fixed values that cannot be modified during the execution of a
program. They are also known as literals. In Java, constants are defined using the final keyword,
for example: final int MAX_VALUE = 100;
• Real: A real number is a number that has a decimal point, such as 3.14 or -0.5. In Java, real
numbers are represented by the data types float and double. The type float uses 32 bits of
memory and is accurate to about 7 decimal places, while double uses 64 bits of memory and is
accurate to about 15 decimal places.

• Single character: A single character is a single letter, digit, or symbol. In Java, characters are
represented by the data type char. Characters are enclosed in single quotes, like this: 'a' or '5'.

• String: A string is a sequence of characters. In Java, strings are represented by the data type
String. Strings are enclosed in double quotes, like this: "Hello World".

• Escape sequence: An escape sequence is a combination of characters that represent a special


character or action. For example, the escape sequence "\" is used to represent a single
backslash in a string, and the escape sequence "\n" represents a newline character. Other
examples include "\t" for tab, "\b" for backspace, "\r" for carriage return, and "\f" for form feed.

• Data Types: In Java, data types determine the kind of data that a variable can hold. There are
two kinds of data types in Java: basic data types (integer, floating-point, character, boolean) and
reference types (objects and arrays).

• Basic Data Types: Basic data types are the most basic data types in Java. They include:

1. Boolean: A Boolean data type can hold either the value true or false. It is used to represent
logical values and conditions.

2. char: A char data type can hold a single character, such as a letter or symbol. It uses 16 bits of
memory.

3. byte: A byte data type can hold an 8-bit signed integer value. It uses 8 bits of memory.

4. short: A short data type can hold a 16-bit signed integer value. It uses 16 bits of memory.
5. int: An int data type can hold a 32-bit signed integer value. It uses 32 bits of memory.

6. long: A long data type can hold a 64-bit signed integer value. It uses 64 bits of memory.

7. float: A float data type can hold a 32-bit floating-point value. It uses 32 bits of memory and is
accurate to about 7 decimal places.

8. double: A double data type can hold a 64-bit floating-point value. It uses 64 bits of memory and
is accurate to about 15 decimal places.

9. String: The String data type is used to store a sequence of characters. It is not a primitive data
type, but is treated as one by the Java language.

10. Object: The Object data type is the root of all classes in Java. Every class in Java is a subclass of
the Object class.

11. E Notations: E notation is a way to represent very large or very small numbers in scientific
notation. The letter "E" or "e" is used to represent "times 10 to the power of". For example,
1.23E2 represents 1.23 times 10 to the power of 2, or 123.

• Reference type/User defined type: A reference type is a data type that refers to an object or
array in memory. In contrast to basic data types, which hold the value directly, reference types
hold a reference or address that points to the location of the object or array in memory.
Examples of reference types in Java include objects and arrays.

• Class: A class is a blueprint or template for creating objects. A class defines the data members
(fields or variables) and methods (functions or procedures) that an object of that class can have.
Classes can also have constructors, which are special methods used to create and initialize
objects of that class.
• Data Members: Data members are the variables and fields of a class. They are used to store the
state of an object. Data members can be of any data type, including basic data types and
reference types.

• Methods: Methods are the functions or procedures of a class. They are used to define the
behavior of an object. Methods can take parameters and return values.

• Object: An object is an instance of a class. An object has its own state, defined by its data
members, and its own behavior, defined by its methods.

• Accessing data members and methods: Data members and methods can be accessed using the
dot notation. To access a data member of an object, use the object's name followed by a dot
and the name of the data member. To call a method of an object, use the object's name
followed by a dot and the name of the method, and provide any necessary parameters in
parentheses.

• Variables: A variable is a named storage location that holds data of a specific type. In Java,
variables must be declared with their data type before they can be used. The syntax for
declaring a variable is:

<data type> <variable name>;

For example:

int age;

• Declaring, initializing, assigning, and accessing values: Declaring a variable means to create the
variable and specify its data type. Initializing a variable means to assign an initial value to it
when it is declared. Assigning a value to a variable means to give it a new value after it has been
declared and initialized. Accessing a variable means to read or use its value.

• Scope and lifetime: The scope of a variable is the part of the program where the variable can be
accessed. A variable's lifetime is the period of time during which the variable exists in memory.
In Java, a variable's scope is determined by the block of code in which it is defined. A variable's
lifetime is determined by when it is created and when it is destroyed.
• Symbolic constants: A symbolic constant is a variable whose value cannot be changed after it is
initialized. In Java, symbolic constants are defined using the final keyword.

final int MAX_AGE = 100;

• Type conversion: Type conversion is the process of converting one data type to another. Java
supports two types of type conversion: automatic conversion and type casting. Automatic
conversion is done automatically by the Java

• Automatic conversion: Automatic type conversion is done automatically by the Java compiler
when a smaller data type is converted to a larger data type. For example, converting an int to a
long, or a float to a double.

• Type casting: Type casting is the explicit conversion of one data type to another. It is done by
placing the desired data type in parentheses before the value that is to be converted. For
example, converting a double to an int would be done like this: int x = (int) 3.14;

• Generic type casting: Sometimes you may want to convert an object of one class to another
class, it is called generic type casting.

• Decimal to binary: Decimal to binary conversion is the process of converting a decimal number
to its binary representation. This can be done by repeatedly dividing the decimal number by 2
and noting the remainder.

• Binary to decimal: Binary to decimal conversion is the process of converting a binary number to
its decimal representation. This can be done by multiplying each binary digit by 2 raised to the
power of its position, starting from the rightmost digit.

• Decimal to hex: Decimal to hex conversion is the process of converting a decimal number to its
hexadecimal representation. This can be done by repeatedly dividing the decimal number by 16
and noting the remainder.
• Hex to decimal: Hex to decimal conversion is the process of converting a hexadecimal number to
its decimal representation. This can be done by multiplying each hexadecimal digit by 16 raised
to the power of its position, starting from the rightmost digit.

• Binary to hex: Binary to hex conversion is the process of converting a binary number to its
hexadecimal representation. This can be done by grouping binary digits into groups of 4, then
converting each group to its hexadecimal equivalent.

• Hex to binary: Hex to binary conversion is the process of converting a hexadecimal number to its
binary representation. This can be done by converting each hexadic.

Expression, Statements, & Block:

• An Expression is a combination of variables, operators and method calls that evaluates to a


value.
• A Statement is a complete instruction that tells the program to perform a specific action.
Examples include assignment statements, method calls, and control statements.
• A Block is a group of zero or more statements enclosed in curly braces. Blocks are used to create
a new scope, and to group statements together.

Comments: Comments are non-executable statements that provide information about the code and are
ignored by the compiler. They are useful for documentation and to help other programmers understand
the code. Java supports two types of comments:

• Line comments: Line comments start with two forward slashes "//" and continue until the end
of the line.

// This is a line comment

• Block comments: Block comments start with a forward slash and an asterisk "/" and end with an
asterisk and a forward slash "/". Everything in between is ignored by the compiler.

/* This is a block comment

It can span multiple lines */

• Error Messages: Error messages are messages generated by the compiler or runtime system to
indicate that something is wrong with the code. They can indicate a problem with the syntax,
logic, or configuration of the program.
Types of error: There are three main types of errors that can occur in a Java program:

• Syntax Error: A syntax error occurs when the code does not conform to the rules of the
programming language. The compiler will generate an error message indicating where the
problem is located in the code. Examples include missing semicolons, unmatched brackets, and
incorrect use of keywords.
• Run-time Error: A run-time error occurs when the code is syntactically correct, but there is an
error in the logic of the program that causes it to crash or behave unexpectedly. Examples
include null pointer exceptions, array index out of bounds, and divide by zero errors.
• Logic Error: A logic error occurs when the code is syntactically and semantically correct but it
doesn't produce the expected result. Examples include incorrect calculations, incorrect output,
or not handling all possible cases.

• Operators: Operators are symbols or keywords that perform specific operations on one or more
operands (values or variables).

• Assignment: The assignment operator (=) assigns a value to a variable. For example, the
statement int x = 5; assigns the value 5 to the variable x.

• Arithmetic: Arithmetic operators perform mathematical operations such as addition (+),


subtraction (-), multiplication (*), division (/), and modulus (%).

• Increment and Decrement: The increment operator (++) increases the value of a variable by 1,
while the decrement operator (--) decreases the value of a variable by 1.

• Relational: Relational operators compare the values of two operands and return a boolean value
indicating whether the comparison is true or false. Examples include greater than (>), less than
(<), equal to (==), and not equal to (!=).

• Logical: Logical operators perform logical operations such as and (&&), or (||), and not (!).
• Conditional: The conditional operator (?) is a ternary operator that takes three operands. It
evaluates a boolean expression, and returns the value of the second operand if the expression is
true, or the value of the third operand if the expression is false.

• Bitwise: Bitwise operators perform operations on the individual bits of an operand. Examples
include bitwise AND (&), bitwise OR (|), bitwise XOR (^), and bitwise complement (~).

• Special: Special operators include the instance of operator, which is used to determine if an
object is an instance of a particular class or subclass.

• Operator Precedence and Associativity: Operator precedence determines the order in which
operators are applied in an expression. For example, multiplication is applied before addition.
Operator associativity determines the order in which operators of the same precedence are
applied. For example, the assignment operator (=) is right-associative, meaning that x = y = z is
the same as x = (y = z).

You might also like