JShell in Java
JShell is a Java Read-Eval-Print Loop (REPL) environment that was first included in JDK 9. Developers may test classes, evaluate expressions, and experiment with the Java language instantaneously with this tool, which eliminates the requirement for conventional compilation and execution cycles. It’s very helpful for learning Java, rapidly testing API capabilities, and developing code.
A snippet is the basic unit of code in JShell and might be a class definition, method definition, or local variable declaration. Local variables, methods, and classes can all be defined from within the JShell prompt. Semicolons are used for expressions that are part of code blocks, such as loops or conditional statements. It’s interesting to note that methods and classes defined in JShell do not require access modifiers.
JShell provides several commands to manage your session:
/edit <snippet_name>
: Allows you to modify an existing snippet./set editor <editor_name>
: Changes the default editor (e.g.,emacs
,vi
,nano
)./vars
: Lists all declared variables./methods
: Lists all defined methods./types
: Lists all defined classes and interfaces./exit
: To leave the JShell environment.
Example of JShell interaction:
jshell> String greeting = "Hello JShell!"
greeting ==> "Hello JShell!"
jshell> int length = greeting.length()
length ==> 13
jshell> void sayHello() {
...> System.out.println(greeting);
...> }
| created method sayHello()
jshell> sayHello()
Hello JShell!
jshell> class MyClass {
...> void doSomething() {
...> System.out.println("Doing something...");
...> }
...> }
| created class MyClass
jshell> MyClass obj = new MyClass()
obj ==> MyClass@1f5d625a
jshell> obj.doSomething()
Doing something...
Java Memory Model and Management
Java’s automatic memory management makes programming easier by managing memory allocation and deallocation, hence removing typical problems like memory leaks that afflict languages like C/C++ that require human memory management. The Java Virtual Machine (JVM) is mostly in charge of this; it functions as an abstract computer that lives in your RAM, translating Java bytecode into native machine code and allocating memory.
The JVM separates memory into various regions, the most often talked about of which are:

- Heap Memory: Heap memory is where arrays and all other things are kept. It is subject to trash collection and is a shared memory space for all threads. The JVM automatically recovers its memory when an object is no longer used by any running program components, making it eligible for garbage collection. Although the
finalize()
method is typically discouraged for new programs because of its uncertain nature, it is called when an object is about to be recycled if it is overridden. To check heap consumption, theRuntime
class has methods liketotalMemory()
andfreeMemory()
. It also offersgc()
to recommend (but not ensure) garbage collection. - Stack Memory: A Java program’s private stack is unique to each thread. Method calls, local variables, and references to heap objects are all stored in this memory. A new frame is added to the stack when a method is called; after the method is finished, the local variables inside that frame are destroyed and the frame is popped.
- Permanent Generation (PermGen): This section of the JVM contained class-related metadata, including class definitions, method data, and string pool details, in earlier iterations (before Java 8). Java 8 replaced PermGen with Metaspace, which by default uses native memory, making it more adaptable and less likely to experience metadata-related
OutOfMemoryErrors
.
Because Java’s memory management is automatic, developers can concentrate on the logic of their applications rather than worrying about these little issues.
JavaBeans (Component Model)
A Java software component model is called JavaBeans. It was first introduced with Java 1.1 and offers a standardised method for creating reusable program components. These “Beans,” as they are commonly called, are made to be easily handled by visual application builder tools.
JavaBeans are a software component model used in Java, designed to facilitate the creation and integration of reusable software components. They adhere to a strict specification, making it easy to incorporate them into applications, especially when using visual development tools.
The main reasons for using JavaBeans include:
- Encapsulation: They encapsulate functionality into easily manageable components.
- Rapid Application Development: They enable the quick development of complex applications by combining various, separately developed components.
- Standardized API Design: JavaBeans establish conventions for defining getters and setters for properties, constructors, and event listener APIs, allowing tools to interact with them consistently.
- Serialization: They frequently use serialization, which is crucial for saving graphical components and user-designed interfaces for later use. This also allows objects to be used across different virtual machines.
- Introspection: The design patterns of JavaBeans support introspection, enabling development tools to analyse a bean’s capabilities automatically.
Ultimately, JavaBeans support the creation of powerful, reusable software components within the Java ecosystem.
JavaBeans’ fundamental concept is to divide functionality into separate, reusable components that can be quickly assembled to create larger applications. Examples of JavaBeans include several graphical user interface (GUI) elements in Java, especially those found in the Swing toolkit (such as JButton
or JTextField
).
Important features of JavaBeans include:

- Properties: Getter and setter methods are usually used to access the Bean’s properties, which enable customisation of its appearance and behaviour.
- Events: Beans can initiate events, and listeners, or other components, can sign up to receive and react to these events.
- Methods: Open methods that specify the functionality of the Bean.
- Serialization: The ability to save and restore beans allows their state to endure.
Developers can facilitate rapid application development (RAD) by following the JavaBeans specification to construct modular and interoperable components that fit into different development environments with ease.
Oracle Official Code Standard
Although whitespace and indentation are free-form in Java, readability and maintainability depend on following code standards, including those frequently suggested by Oracle’s documentation.

The following are some important factors:
- Naming Conventions:
- Classes and Interfaces: Typically use nouns, starting with an uppercase letter, following CamelCase (e.g.,
MyClass
,HelloWorldService
). - Methods: Typically use verbs, starting with a lowercase letter, following camelCase (e.g.,
main()
,sayHello()
). - Variables: Start with a lowercase letter, following camelCase (e.g.,
myVariable
,userName
). Parameters are also variables. - Constants (final variables): All uppercase, with words separated by underscores (e.g.,
MAX_VALUE
,PI
). - Packages: Use lowercase letters, and sometimes underscores (e.g.,
java.lang
,com.example.mypackage
).
- Classes and Interfaces: Typically use nouns, starting with an uppercase letter, following CamelCase (e.g.,
- Class Structure:
- The
main()
function serves as the program’s starting point and is found in one of the classes that make up a Java program. - Data (instance variables/fields) and the code that manipulates that data (methods) are contained in classes.
- The
System
class, which gives access to system resources and standard output (System.out
), is predefined and automatically included.
- The
- Modifiers:
- The order of modifiers is generally: Access modifier (public, private, protected), followed by
abstract
,static
,final
,transient
,volatile
,default
,synchronized
,native
,strictfp
. - In interfaces, methods’
public
andabstract
modifiers are implicit and frequently left out. - The
static
keyword indicates that a member is a class member, which means that regardless of object instances, there is only one copy.
- The order of modifiers is generally: Access modifier (public, private, protected), followed by
- Braces and Indentation:
- The use of braces and indentation Blocks of code are separated by curly braces
{}
. Classes, methods, and control flow structures (such asif
,for
, andswitch
) are grouped together in these statements. - Java is a free-form language, which means that the compiler does not rigidly enforce indentation and whitespace constraints. Consistent indentation, such as two or four spaces, greatly enhances readability, nevertheless. For a better understanding of control flow and how to reformat code using spaces and line breaks.
- Braces can be placed however you like, but it’s important to use them consistently across a project.
- The use of braces and indentation Blocks of code are separated by curly braces
- Whitespace:
- The Java compiler mostly ignores whitespace characters, which are essential for human reading and include spaces, tabs, and newlines.
- Using blank lines can assist in separating logical code segments.
- Statements:
- A semicolon (;) must be used to end every Java statement. This is a basic syntactic principle.
Following these guidelines elevates programming to the level of an art form by making code simpler to comprehend, debug, and collaborate on. Even if there are numerous inventive ways to use the instruments themselves, it’s like having a well-organised toolbox; you know precisely where everything is.