For working professionals
For fresh graduates
More
Step by Step Java Tutorial Con…
1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
Now Reading
21 mins read
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Discover the fundamentals of Java programming with this Java tutorial for beginners. In this tutorial, we will cover the fundamentals of Java and the types of applications that can be developed with this language with various Java examples.
This comprehensive guide will also cover the different Java platforms or editions to help you become a proficient Java developer.
Java is a widely used, high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle) in the mid-1990s. It is designed to be platform-independent, allowing programs written in Java to run on any system with a Java Virtual Machine (JVM) installed. This "Write Once, Run Anywhere" (WORA) principle makes Java highly versatile.
Java also bears a similarity in syntax to the C and C++ programming languages. This, in turn, makes a Java tutorial for beginners easy to master for programmers familiar with these languages. The interface is known for its simplicity, readability, and robustness. Java's object-oriented nature allows for modular and reusable code, making it ideal for building complex applications.
One of Java's key features is its automatic memory management through garbage collection, which helps developers avoid memory leaks and ensures efficient memory usage. Additionally, Java offers a rich set of libraries and frameworks that simplify tasks such as network programming, database access, and graphical user interface (GUI) development.
Java is widely used in various domains, including enterprise software development, web development, mobile app development (with Android), scientific research, and more. Its popularity stems from its stability, portability, and extensive community support, making it an excellent choice for beginner and experienced programmers.
Let us check a few common types of Java applications to understand how this programming language can be used in various ways.
Here is a simple console-based Java example that prints "Hello, World!" to the console when executed:
The provided code snippet represents a basic Java console application. It defines a public class named ExampleApp with a main method as the entry point.
The main method is the starting point of the program and is declared with public static void main() and takes an array of strings (String[] args) as a parameter, representing command-line arguments.
Within the main method, the statement System.out.println("Hello, World!"); is used to display the string "Hello, World!" as output on the console. This code demonstrates the fundamental structure of a console application and illustrates how to print text to the console using the System.out.println() method.
Here is a basic Java GUI application using Swing that creates a window with a button displaying "Click Me" when executed:
The code defines a class named GuiApp with a main method. It creates an instance of JFrame to represent a window and adds a JButton. The window's title is set, and its size and visibility are also configured.
Here is an example that shows a simple Java servlet that responds with an HTML page containing the text "Hello, World!" when accessed:
The code showcases a class named HelloServlet that extends HttpServlet. It overrides the doGet method to handle HTTP GET requests. It sets the response content type to HTML and writes an HTML response containing the message "Hello, World!". This code presents a basic Java servlet for generating dynamic web content.
Here is an example that demonstrates a JavaFX desktop application. It creates a window with a label displaying "Hello, World!" when executed:
The code demonstrates a JavaFX desktop application. It extends the Application class, overriding the start method to configure the user interface. It creates a window with a "Hello, World!" label. The application's title, scene, and stage are set, and the window is displayed.
Here is a code snippet from an Android application that sets the content view to a TextView displaying "Hello, World!" on the screen:
The code showcases an Android application's main activity, MainActivity. It extends the Activity class, overriding the onCreate method to configure the user interface. It creates a TextView and sets its text to "Hello, World!". The TextView is set as the content view of the activity.
Here is an example that demonstrates a basic Java database application using JDBC (Java Database Connectivity). This program establishes a connection to a MySQL database, executes a query, and retrieves data from the result set:
Here is an example that demonstrates a multithreaded Java application:
In the MultithreadedApp class, the main method serves as the entry point of the program. It creates two threads, thread1 and thread2, by passing instances of the MyRunnable class to the Thread constructor. This allows the threads to execute the run method defined in MyRunnable.
The MyRunnable class implements the Runnable interface, which requires implementing the run method. Within the run method, a loop is executed five times. In each iteration, the method prints a message that includes the thread ID (obtained using Thread.currentThread().getId()) and a count value.
Both threads start with the start method when the program is run. As a result, the run method of each thread is executed concurrently. This leads to the interleaved printing of messages from different threads, each with its unique ID.
The program's output may vary on each run, but it typically displays the thread ID and count values interleaved. This demonstrates the concurrent execution of multiple threads in a multithreaded application.
Code:
public class MultithreadedApp {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable());
Thread thread2 = new Thread(new MyRunnable());
thread1.start();
thread2.start();
}
}
class MyRunnable implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread: " + Thread.currentThread().getId() + ", Count: " + i);
}
}
}
Here is a Java example that showcases a basic client-server network application:
(ServerApp.java file)
(ClientApp.java file)
The ServerApp class represents a server-side application. It creates a ServerSocket to listen on port 8080 for incoming client connections. When a client connects, the server accepts the connection and reads the client's message using a BufferedReader. It then prints the received message and responds to the client using a PrintWriter. Finally, it closes the reader, writer, client, and server sockets.
The ClientApp class represents a client-side application. It creates a Socket to connect to the server at "localhost" on port 8080. It sends a message to the server using a PrintWriter and reads its response using a BufferedReader. The received response is printed, and then the reader, writer, and socket are closed.
Code for the ServerApp.java file:
import java.io.*;
import java.net.*;
public class ServerApp {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("Server started. Listening on port 8080...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String message = reader.readLine();
System.out.println("Received message: " + message);
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
writer.println("Message received!");
reader.close();
writer.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server Output:
Code for the ClientApp.file:
import java.io.*;
import java.net.*;
public class ClientApp {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8080);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hello, Server!");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = reader.readLine();
System.out.println("Server response: " + response);
writer.close();
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client output:
Java offers different platforms or editions, each tailored to meet specific types of Java applications and domains. These platforms provide a range of tools, libraries, and APIs to support developers in their respective focus areas.
This edition provides the core components and libraries necessary for general-purpose Java development. It includes features for desktop applications, command-line tools, and server-side applications.
Java EE is designed for developing large-scale, enterprise-level applications. It extends Java SE by adding additional APIs and frameworks for distributed computing, web services, and enterprise application integration.
Java ME is optimized for resource-constrained devices, such as mobile phones, embedded systems, and IoT devices. It offers a subset of Java SE functionality to accommodate the limitations of these devices.
JavaFX is a platform for building rich, cross-platform desktop applications with modern user interfaces. It provides a set of UI controls, multimedia support, and 3D graphics capabilities.
These platforms allow developers to choose the most suitable edition for their project requirements, ensuring flexibility and scalability across various domains and device types.
Whether you're interested in developing desktop applications, enterprise software, mobile apps, or even exploring the Internet of Things (IoT), Java has got you covered. Its WORA capability, extensive libraries, and strong community support make it an excellent choice for beginners and experienced developers.
Learning Java opens up a world of possibilities and provides a solid foundation for a successful career in programming. So, grab your favorite IDE, dive into Java, and prepare to embark on an exciting and rewarding journey. Programming can sometimes be challenging, but with Java, you have a powerful tool to bring your ideas to life. Happy coding!
1. Is Java difficult to learn for beginners?
Java can be challenging for beginners due to its syntax and object-oriented nature. However, with proper guidance and practice, it is considered one of the more beginner-friendly languages. Its extensive documentation, online resources, and active community make it easier to find help and support while learning.
2. Can I use Java for web development?
Yes, Java is widely used for web development. Java frameworks like Spring and JavaServer Faces (JSF) provide powerful tools for building robust and scalable web applications. Additionally, Java's compatibility with databases and support for server-side technologies make it a popular choice for developing dynamic websites and web services.
3. What is the future of Java?
Java continues to be a popular and in-demand programming language. With its versatility, portability, and wide adoption, the future of Java looks promising. Java's compatibility with emerging technologies like cloud computing, big data, and machine learning ensures its relevance in the ever-evolving tech industry. Additionally, the active Java community and regular updates from Oracle ensure that Java remains up-to-date and equipped to tackle future challenges.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.