If you aim to learn core java and have a strong foundation of basic java learning, you must master core java basics. Whichever core java course begins with explaining the basics of Java core and then proceeds to the core and advanced java concepts. The core java learning path includes all the sections discussed here.
So, if you want to learn core java and master core java programming, clear the following fundamental concepts:
5.1 Variables in Java
Variables are defined in the form of a location in the system memory where values are saved. Also recognized as identifiers, each variable has a name, a value, and a type.
5.2 Data types in Java
They represent the types of data that a value can have. Two primary data types are built-in/primitive data types and user-defined data types.
The subtypes of primitive data types in Java are byte, short (2 bytes), integer (4 bytes), long (8 bytes), float (4 bytes), character (2 bytes), double (8 bytes), and boolean(1 bit). The user-defined data types include class, object, array, string, and interface. The later section of the core java basics will explain these data types in detail.
5.3 Keywords in Java
The words with unique meanings in Java programming are called keywords. Examples include main, int, static, public, void, etc. Plenty of keywords exist in Java - you can readily find them on the internet for core java programming.
5.4 Operators in Java
Operators in Java assist you in carrying out various kinds of operations. It is impossible to clear the java basic concepts for beginners without learning the operators. Seven types of operators in Java are discussed below.
1. Arithmetic operators:
They assist you in performing arithmetic operations. Two operands are required. The list of arithmetic operators: +, -, /, *,% (MOD).
2. Logical operators:
If the need arises to carry out operations on logical combinations of boolean (true/false) values then logical operators are useful. These binary operators use two operands. Three logical operators are used in Java - &&(AND), ||(OR), !(NOT).
3. Relational operators:
Also known as comparison operators, they allow you to compare any two values. Relational operators in Java are <, <=, >, >=, ==, !=.
4. Assignment operators:
If you want to assign values of any expression to a specific variable, then assignment operators are used. It also covers a shorthand operator. The assignment operators in Java are: =, +=, -=, *=, /=, %=.
5. Bitwise operators:
Bitwise operators help to perform operations on data at the bit level. Some of the bitwise operators in Java are & (AND), | (OR) , ^ (NOT), >> (Right Shift), << (Left Shift), etc.
6. Increment and decrement operators:
They allow you to increase or decrease the value of a variable by 1. Two types are available, i.e., pre and post, and they are named pre-increment (++x) or pre-decrement (–x).
7. Ternary operators:
Also recognized as conditional operators, they work on three operands. It executes such that it evaluates expression1 if the condition is true else expression2 will be assessed if the condition turns false.
5.5 String handling
Understanding all the concepts of Core Java is essential if you aim to learn complete core java in a simple way. One of the most important concepts is String Handling.
In Java, the String is a class. Every object and string of this class represents a sequence of characters written within double-quotes. Moreover, the String class is handy for creating and working on strings. If you want to store text in Java, you can use strings. They work identically as an array of char values.
Examples of strings can be any word like “hello”. In Java, the string is immutable. So, once prepared, their values remain unchanged.
The following example shows how to create a String:
class Strings {
public static void main (String args[]) {
String s = "Learn Java";
System.out.println(s);
}
}
The output will be: Learn Java
String Handling takes place with the use of String Class Methods. Various methods are used in strings, and stored in java.lang.String class in which java.lang is a package.
1. int length()
2. int compareTo(String)
3. int compareToIgnoreCase(String)
4. boolean equals(String)
5. boolean equalsIgnoreCase(String)
6. String concat(String)
7. String toUpperCase()
8. String toLowerCase()
9. char charAt(int)
10. String format()
11. String substring()
12. boolean contains(String)
13. String replace(String1, String2)
14. String replaceFirst(string1, string2)
15. String replaceAll()
16. int indexOf(String)
17. String trim()18. String valueOf()
18. String valueOf()
19. boolean startsWith(String)
20. boolean endsWith(String)
21. int lastIndexOf()
22. boolean isEmpty()
23. int hashCode()
24. String[] split()
25. char[] toCharArray()
26. String toString()
27. String join()
5.6 Exception handling
An undesirable event that takes place during execution is known as an exception. It disturbs the usual flow of the program, which usually leads to the termination of the app or program.
There can be various reasons behind the generation of exceptions in Java. Some of them are unacceptable data input, attempting to access an array element with an index higher than the size of the array, trying to access a non-existing file in a program, network issues, etc. But Java boasts a robust system for dealing with these errors and ultimately avoiding the termination of the specific program.
Whenever Java encounters an exception, an object named ‘Exception object’ is created and assigned to the Java-Runtime System (JVM). The whole program is searched to match the exception handler with the type of exception that occurred. If nothing is found, the default exception handler will show a message to the user stating the information of the exception that took place.
The core java programming examples for exception handling help you understand its concept better. For instance, if you divide a number by zero. It will lead to the generation of exceptions.
5.7 MultiThreading
Several processes (instances) can exist in an application. All these processes can be handed off as a single thread or multiple threads. Multithreading helps to carry out numerous tasks simultaneously.
In Java, a single thread is the smallest and most lightweight element of processing. Note that Java uses threads through a “Thread Class”. On the other hand, multithreading is a process of operating two or more threads concurrently. It is also known as concurrency in Java.
Unique aspects of multithreading in Java:
- It can maximize CPU utilization.
- Every thread runs parallel to one another.
- Multiple threads don’t assign separate memory areas, so they save memory.
- They don’t block users since threads are independent.
- Because threads are independent, other threads will not be affected if one thread encounters an exception.
5.8 Applet
An applet is a unique form of Java program which operates in a Java-enabled browser. Typically, the applet is embedded in a web page and operates in the browser.
They are tiny Java applications accessible on an Internet server and conveyed through the Internet. Moreover, they can be automatically installed and executed as an element of a web document.
Once a user receives an applet, it can generate a graphical user interface. The partial access to resources allows it to execute complex calculations without any risk of data integrity breaches or viruses.
A class should extend java.applet.Applet class to create an applet. Note that an Applet class doesn’t possess any main() method; it is observed with the help of JVM.
Here is an example demonstrating the use of Applet in Java:
import java. awt.*;
import java.applet.*;
public class ABC extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello", 30, 30);
}
}
Here, the height and width statements denote the dimensions of the display area being used by the applet.
5.9 AWT & EventHandling
The working of Abstract Window Toolkit (AWT) is based on event-driven programming that processes user actions. These actions trigger all contemporary window systems programming. In the AWT, every user action relates to an abstract set of items known as events.
An event narrates a specific user action, and the program actively collects user-generated events. Moreover, the Java runtime informs the program whenever a unique event takes place. Those programs that manage user interaction in this way are termed event-driven.
Four types of classes provided by Event Handling are:
- Event Adapters
- Event classes
- Event Sources
- Event Listeners
5.10 Datatypes and variables
A variable in Java is the name of a reserved area assigned in memory. In other words, it is the name of a memory location.
Here is the example:
int var = 15 //here var is variable
Note that in Java, all the variables should be declared before being used. The syntax to declare a variable in Java is:
type identifier [ = value][, identifier [= value] ...] ;
Types of variables:
1. Local variable: Declared in the method
2. Instance variable: Declared in methods, blocks, or constructors.
3. Static variable: Declared as static; it can’t be local.
4. Instance variable: Declared within the class but outside the method; it is not declared static.
Type is a Java data type, and the identifier is the name of a variable. If you want to declare multiple variables of a particular type, use commas in the list.
Data types in Java
There are two types of data types in Java, they are:
1.Primitive data types:
Java supports 8 primitive data types: short, byte, int, float, long, double, char, and Boolean. They are predefined by the language and titled by a keyword.
2. Non-primitive data types:
Also known as reference data types, they allow the creation of reference variables with the help of defined constructors of particular classes. The value of corresponding variables can’t be changed. Moreover, they are helpful for accessing objects.
Objects, classes, and various array variables are included in the non-primitive data types. Note that the default value of a reference variable is null.
5.11 Statement and arrays
Statement:
Java permits the users to use two different array-specific syntax shortcuts. Both of them are used to initialize and construct a single statement. The syntax of a statement in Java includes two statements. The first statement declares, creates, and initializes in one statement. The second one declares, creates, populates, and assigns.
For example,
int y=45;
int[] number={10, 14, 70, y, 55};
Arrays:
Java arrays are objects consisting of elements of identical data types. They are data structures for storing identical elements. Moreover, they are objects of the dynamically generated class. An array’s elements are saved in adjacent memory locations. Note that you can only store a finite set of elements.
Being index-based, the first element is stored at the 0th index, and the second element is on the first index, and so on.
Java array accepts the Object class and employs the Cloneable and Serializable interfaces. Like Java, you can store objects or primitive values in an array. Furthermore, you can also make single-dimensional or multidimensional arrays in Java.
Key benefits of using Arrays in Java:
- The code optimization lets you efficiently fetch or sort the data.
- You can obtain any data situated at an index position with random access.
5.12 I/O Basics and Streams
Understanding I/O basics and streams is crucial if you want to get the basic knowledge of java. The java.io package comprises almost all the classes you may require to carry out Java's input and output (I/O) operations. A stream is a sequence of data. All such streams depict an input source and an output destination. Furthermore, the stream in the java.io package can support different data like localized characters, objects, primitives, etc.
A core java developer preparing for a basic java interview may be asked questions on I/O streams. So, here is the meaning of each of them.
InputStream: It is used to read data from a source.
OutputStream: It is used for writing data to a particular destination.
5.13 Threads:
Threads are used with the help of a ‘Thread Class’.
Two types of thread in Java are:
i. User thread
ii. Daemon thread
Whenever an application launches, a user thread is created. Subsequently, you can generate several daemon threads and user threads.
Example of creating a single thread:
public class SampleThread
{
public static void main(String[] args) {
System.out.println("An Example Thread");
}
}
The output will be: An Example Thread
5.14 Collections framework
The Java platform supports a collection framework. A collection is a cluster of objects denoted as a single unit. The collection framework in Java is an architecture that represents, stores, and manipulates data of the collections. Furthermore, the Java Collection framework defines many different interfaces and classes in a way that a group of objects is denoted as one unit.
With the introduction of the Collection Framework in the JDK 1.2, all collections are merged. It also offered a shared interface for them. Besides, it permitted various collections to work identically and with great interoperability. The collection framework ascertains that collections can be independently manipulated regardless of their implementation particulars. The basis of the Collection framework is dependent on the Collection interface. Hence, it comprises all the declarations of the methods the Collections would have.
Syntax of making objects of all Collection framework’s classes is identical, it is:
CollectionClassName <dataType> objName = new CollectionClassName()
Need for a collection framework:
An individual preparing to become a core java developer must know the requirement for a collection framework. Before the introduction of the Java Collections framework, the typical collections used were Hashtables, Arrays, and Vectors. But they didn’t have any shared interface and lacked a coalescing theme. Moreover, each structure had varied methods and syntax for retrieving and manipulating data. As a result, it created issues and a reduction in efficiency.
Advantages of using the Collection framework:
- It decreases programming effort because algorithms and data structures are readily available.
- It is performance efficient and can effortlessly extend and adjust to all collections.
- It allows Java developers to concentrate on applying these collections rather than worrying about their execution.
- It decreases the effort needed to learn, create, and implement APIs (Application Program Interface).
Types of Collection Interfaces:
- List
- ArrayList
- LinkedList
- Vector
- Stack
- Queue
- PriorityQueue
- Deque
- Set
- LinkedHashSet
- HashSet
- TreeSet
- SortedSet
- Map
- HashMap
- HashTable
- LinkedHashMap
- TreeMap
- SortedMap