Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Hello World Program

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Hello World Program

      Printing "Hello World" on the output screen (console) is the first program in Java and other programming languages. This tutorial will teach you how you can write your first program (print "Hello World" program) in Java programming.

      Java program to print "Hello World"

      Java program to print "Hello World" is given below:
      public class MyFirstJavaProgram {
      
      /* This is my first java program.
      * This will print 'Hello World' as the output
      */
      
      public static void main(String []args) {
      System.out.println("Hello World"); // prints Hello World
      }
      }

      Steps to Write, Save, and Run Hello World Program

      Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps
      • Open notepad and add the code as above.
      • Save the file as − "MyFirstJavaProgram.java".
      • Open a command prompt window and go to the directory where you saved the class. Assume it's C:\.
      • Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there is no error in your code, the command prompt will take you to the next line (Assumption − The path variable is set. Learn: Java Envionment Setup).
      • Now, type 'java MyFirstJavaProgram' to run your program.
      • You will be able to see "Hello World" printed on the screen.

      Output

      C:\> javac MyFirstJavaProgram.java
      C:\> java MyFirstJavaProgram
      Hello World

      Explanation of Hello World Program

      As we've successfully printed Hello World on the output screen. Let's understand the code line by line.

      1. Public Main Class

      public class MyFirstJavaProgram {
      This line is creating a new class MyFirstJavaProgram and being public, this class is to be defined in the same name file as MyFirstJavaProgram.java. This convention helps Java compiler to identify the name of public class to be created before reading the file content.
      

      2. Comment Section

      /* This is my first java program.
      * This will print 'Hello World' as the output
      */
      These lines being in /* */ block are not considered by Java compiler and are comments. A comment helps to understand program in a better way and makes code readable and understandable.
      

      3. Public Static Void Main

      public static void main(String []args) {
      This line represents the main method that JVM calls when this program is loaded into memory. This method is used to execute the program. Once this method is finished, program is finished in single threaded environment.

      4. Keywords Used

      Let's check the purpose of each keyword in this line.
      • public − defines the scope of the main method. Being public, this method can be called by external program like JVM.
      • static − defines the state of the main method. Being static, this method can be called by external program like JVM without first creating the object of the class.
      • void − defines the return type of the main method. Being void, this method is not returning any value.
      • main − name of the method
      • String []args − arguments passed on command line while executing the java command.

      5. System.out.println() Method

      System.out.println("Hello World"); // prints Hello World
      System.out represents the primary console and its println() method is taking "Hello World" as input and it prints the same to the console output.