AJP Unit I
AJP Unit I
JAVA FUNDAMENTALS
Java brings various Streams with its I/O package that helps the user to perform all the input-
output operations. These streams support all the types of objects, data-types, characters, files
etc to fully execute the I/O operations.
Before exploring various input and output streams lets look at 3 standard or default
streams that Java has to provide which are also most common in use:
1. System.in: This is the standard input stream that is used to read characters from the
keyboard or any other standard input device.
2. System.out: This is the standard output stream that is used to produce the result of a
program on an output device like the computer screen.
Here is a list of the various print functions that we use to output statements:
print(): This method in Java is used to display a text on the console. This text is passed
as the parameter to this method in the form of String. This method prints the text on
the console and the cursor remains at the end of the text at the console. The next
printing takes place from just here.
Syntax:
System.out.print(parameter);
Example:
// Java code to illustrate print()
import java.io.*;
class Demo_print {
public static void main(String[] args)
{
// using print()
// all are printed in the
// same line
System.out.print("GfG! ");
System.out.print("GfG! ");
System.out.print("GfG! ");
}
}
Output:
GfG! GfG! GfG!
println(): This method in Java is also used to display a text on the console. It prints the text
on the console and the cursor moves to the start of the next line at the console. The next
printing takes place from the next line.
Syntax:
System.out.println(parameter);
Example:
// Java code to illustrate println()
import java.io.*;
class Demo_print {
public static void main(String[] args)
{
// using println()
// all are printed in the
// different line
System.out.println("GfG! ");
System.out.println("GfG! ");
System.out.println("GfG! ");
}
}
Output:
GfG!
GfG!
GfG!
printf(): This is the easiest of all methods as this is similar to printf in C. Note that
System.out.print() and System.out.println() take a single argument, but printf() may take
multiple arguments. This is used to format the output in Java.
Example:
// A Java program to demonstrate working of printf() in Java
class JavaFormatter1 {
public static void main(String args[])
{
int x = 100;
System.out.printf( "Printing simple" + " integer: x = %d\n", x);
float n = 5.2f;
n = 2324435.3f;
Syntax
Types of Streams:
Depending on the type of operations, streams can be divided into two primary classes:
1. Input Stream: These streams are used to read data that must be taken as an input from
a source array or file or any peripheral device. For eg., FileInputStream,
BufferedInputStream, ByteArrayInputStream etc.
2. Output Stream: These streams are used to write data as outputs into an array or file or
any output peripheral device. For eg., FileOutputStream, BufferedOutputStream,
ByteArrayOutputStream etc.
Depending on the types of file, Streams can be divided into two primary classes which can
be further divided into other classes as can be seen through the diagram below followed
by the explanations.
1. ByteStream: This is used to process data byte by byte (8 bits). Though it has many
classes, the FileInputStream and the FileOutputStream are the most popular ones. The
FileInputStream is used to read from the source and FileOutputStream is used to write
to the destination. Here is the list of various ByteStream Classes:
2. CharacterStream: In Java, characters are stored using Unicode conventions (Refer this
for details). Character stream automatically allows us to read/write data character by
character. Though it has many classes, the FileReader and the FileWriter are the most
popular ones. FileReader and FileWriter are character streams used to read from the
source and write to the destination respectively. Here is the list of various
CharacterStream Classes:
Implementation:
1. Filtering out the elements divisible by some specific number ranging between 0 to 10.
2. Filtering out the elements with an upperCase letter at any specific index.
3. Filtering out the elements ending with custom alphabetical letters.
Example 1: filter() method with the operation of filtering out the elements with an upperCase
letter at index 1.
import java.util.stream.Stream;
// Class
class GFG {
The following code Creates piped input and outpu t streams and connect them
Syntax
Byte code is an intermediate code that is created by the compiler after compiling the user-
written code and we can run that intermediate code anywhere with the help of a Java Virtual
machine. Java bytecode is a low-level representation of Java code, consisting of a series of
instructions for the JVM to execute.
The main advantage of Java’s use of bytecode is that the JVM can optimize the code for the
specific machine it’s running on. This can result in improved performance compared to the
same code compiled directly to machine code. The JVM can also perform various checks and
optimizations during runtime, such as garbage collection, dynamic class loading, and security
checks.
Advantages of Byte Code in Java
1.4 Reflection
1. Class : To know the name of the class to which that particular object belongs we can use
the getClass() method.
2. Constructors : To know the public constructors of the class to which that particular
object belongs we can use the getConstructors() method.
3. Methods : To know the public methods of the class to which that particular object
belongs we can use the getMethods() method.
Example
1. Using forName() method : This method is used to load the class dynamically and return
the instance of the Class class. The forName() method cannot be used for primitive
types.
class Animal { }
class Dog
{
public static void main ( String args[] )
{
Class a = Class.forName ( "Animal" );
System.out.println ( a.getName() );
}
} Output : Animal
Dynamic class loading in Java refers to the process of loading and using classes at runtime,
instead of loading them during the static compilation phase. By dynamically loading classes, Java
programs gain the ability to load and execute code on-demand, thereby enabling the creation of
more flexible and extensible applications.
Java provides a rich set of APIs and mechanisms to dynamically load classes, such as the
Class.forName() method, ClassLoader instances, and reflection.
try {
Class<?> dynamicClass = Class.forName("com.example.DynamicClass");
dynamicClass.getMethod("someMethod").invoke(dynamicObject);
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
e.printStackTrace();
}
In the above example, we load the class com.example.DynamicClass dynamically at runtime
using Class.forName(). We then instantiate an object of the loaded class and invoke a method
called someMethod(). This allows us to execute code that was not known at compile-time,
bringing runtime flexibility to our Java application.
Threads allows a program to operate more efficiently by doing multiple things at the same
time.
Threads can be used to perform complicated tasks in the background without interrupting the
main program.
Typically, we can define threads as a subprocess with lightweight with the smallest unit of
processes and also has separate paths of execution. These threads use shared memory but
they act independently hence if there is an exception in threads that do not affect the
working of other threads despite them sharing the same memory.
// Declare an instance native method sayHello() which receives no parameter and returns void
private native void sayHello();
// Test Driver
public static void main(String[] args) {
new HelloJNI().sayHello(); // Create an instance and invoke the native method
}
}
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-
based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Swing is a Set Of API ( API- Set Of Classes and Interfaces )
Swing is Provided to Design Graphical User Interfaces
Swing is an Extension library to the AWT (Abstract Window Toolkit)
Includes New and improved Components that have been enhancing the looks and
Functionality of GUIs’
Swing is more portable and more flexible than AWT, The Swing is built on top of the AWT
Swing is Entirely written in Java
Java Swing Components are Platform-independent And The Swing Components are
lightweight
// Main class
class GFG {
Output: