notes @JAVA
notes @JAVA
Java was developed by James Gosling at Sun Microsystems Inc in May 1995 and later
acquired by Oracle Corporation. It is a simple programming language. Java makes writing,
compiling, and debugging programming easy. It helps to create reusable code and modular
programs. Java is a class-based, object-oriented programming language and is designed to
have as few implementation dependencies as possible. A general-purpose programming
language made for developers to write once run anywhere that is compiled Java code can run
on all platforms that support Java.
1. Platform Independent
2. Object-Oriented Programming
3. Simplicity
4. Robustness
5. Security
6. Distributed
7. Multithreading
8. Portability
9. High Performance
10. Distributed
The program creation:
The Java program can be written using a Text Editor (Notepad++ or NotePad or other
editors will also do the job.) or IDE (Eclipse, NetBeans, etc.).
FileName: TestClass.java
Write the above code and save the file with the name TestClass. The file should have
the .java extension.
Open the command prompt, and type javac TestClass.java. javac is the command that
makes the Java compiler come to action to compile the Java program. After the command, we
must put the name of the file that needs to be compiled. In our case, it is TestClass.java. After
typing, press the enter button. If everything goes well, a TestClass.class file will be generated
that contains the byte code. If there is some error in the program, the compiler will point it
out, and TestClass.class will not be created.
SCANNER CLASS
The Scanner class in Java is part of the java.util package and is used to parse primitive
types and strings using regular expressions. It can be used to read input from various sources,
including user input from the console, files, and streams.
Input Sources: It can read data from different sources like InputStream, File, String, and
Readable.
Tokenization: The Scanner can split input into tokens based on whitespace or specified
delimiters.
Data Types: It provides methods to read various data types, such as int, float, double, String,
etc.
Creating a Scanner:
Reading Input:
if (scanner.hasNextInt()) {
Closing the Scanner: It's good practice to close the scanner to free up resources.
scanner.close();
DATA TYPES IN JAVA
Java is statically typed and also a strongly typed language because, in Java, each type
of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined
as part of the programming language and all constants or variables defined for a given
program must be described with one of the Java data types.
Data types in Java are of different sizes and values that can be stored in the variable
that is made as per convenience and circumstances to cover up all test cases. Java has two
categories in which data types are segregated.
1. Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double.
The Boolean with uppercase B is a wrapper class for the primitive data type boolean
in Java.
2. Non-Primitive Data Type (or) Object Data type: such as String, Array, etc.
Primitive data are only single values and have no special capabilities. There are 8
primitive data types. They are depicted below in tabular format below as follows:
The boolean data type represents a logical value that can be either true or false.
Conceptually, it represents a single bit of information, but the actual size used by the virtual
machine is implementation-dependent and typically at least one byte (eight bits) in practice.
Values of the boolean type are not implicitly or explicitly converted to any other type using
casts. However, programmers can write conversion code if needed.
The byte data type is an 8-bit signed two’s complement integer. The byte data type is
useful for saving memory in large arrays.
The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a
short to save memory in large arrays, in situations where the memory savings actually
matters.
The range of a long is quite large. The long data type is a 64-bit two’s complement
integer and is useful for those occasions where an int type is not large enough to hold the
desired value. The size of the Long Datatype is 8 bytes (64 bits).
The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float
(instead of double) if you need to save memory in large arrays of floating-point numbers. The
size of the float data type is 4 bytes (32 bits).
The double data type is a double-precision 64-bit IEEE 754 floating-point. For
decimal values, this data type is generally the default choice. The size of the double data type
is 8 bytes or 64 bits.
The char data type is a single 16-bit Unicode character with the size of 2 bytes (16
bits).
Possible errors:
1. java:7: error: incompatible types: possible lossy conversion from float to int
int floatValue = 5.75f;
2. java:11: error: package system does not exist
system.out.println("Byte Value: " + byteValue);
3. java:8: error: ';' expected
double doubleValue = 19.99
The Non-Primitive (Reference) Data Types will contain a memory address of variable
values because the reference types won’t store the variable value directly in memory. They
are strings, objects, arrays, etc.
1.Strings:
Strings are defined as an array of characters. The difference between a character array
and a string in Java is, that the string is designed to hold a sequence of characters in a single
variable whereas, a character array is a collection of separate char-type entities. Unlike C/C+
+, Java strings are not terminated with a null character.
Syntax: <String_Type> <string_variable> = “<sequence_of_string>”;
Possible errors:
1. incompatible types: String cannot be converted to int
int second = "Python";
2. java:2: error: <identifier> expected
class {
2.Class:
3. Object
3. Identity : It gives a unique name to an object and enables one object to interact with
other objects.
4. Interface
Like a class, an interface can have methods and variables, but the methods declared in an
interface are by default abstract (only method signature, no body).
Interfaces specify what a class must do and not how. It is the blueprint of the
class.
An Interface is about capabilities like a Player may be an interface and any
class
implementing Player must be able to (or must implement) move(). So it
specifies a set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all
functions specified in the interface, then the class must be declared abstract.
A Java library example is Comparator Interface . If a class implements this
interface, then it can be used to sort a collection.
5. Array
An Array is a group of like-typed variables that are referred to by a common name.
Arrays in Java work differently than they do in C/C++. The following are some important
points about Java arrays.
class Main {
public static void main(String[] args) {
int[] age = {12, 4, 5, 2, 5};
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
}
}
Output:
Accessing Elements of Array:
First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Possible errors:
1.Local Variables:
Possible errors:
1. java:3: error: cannot find symbol
string number = 5;
2. java:10: error: reached end of file while parsing
3. }
2. Instance Variables:
Instance variables are non-static variables and are declared in a class outside of any
method, constructor, or block.
As instance variables are declared in a class, these variables are created when an
object of the class is created and destroyed when the object is destroyed.
Unlike local variables, we may use access specifiers for instance variables. If we do
not specify any access specifier, then the default access specifier will be used.
3. Static Variables:
Possible errors:
1. java:12: error: ';' expected
Counter counter3 = new Counter()
2. java:3: error: '(' expected
private static void count = 0;