Java Tutorials

100+ tutorials, explanation videos, and examples to help you develop creative and technical skills.

Java basic syntax and first program in Java?

Java Basic Syntax

In java, we use classes to interact with methods in our java program. These classes consists of Methods(Functions), Variables etc. Every method and variable must be declared within a class otherwise it won’t run and give error.

First we use access modifiers (public, private, protected). We will discuss access modifiers in next chapters.

Class Name – ‘TextProgram’ is a class name. Its first letter should be uppercase and this will not cause an error even all letters are small but java suggest always use first letter of a word in uppercase. For example here ‘T’ of ‘Text’ and ‘P’ of ‘Program’ is uppercase.

Java is case-sensitive : ‘TextProgram’ and textProgram and textprogram have different meanings.

Every java program has a main class which has main() function like below. So in our program ‘TextProgram’ is a main class. In java, code written outside the main function won’t execute.

Every java program has a main class which has main() function like below. So in our program ‘TextProgram’ is a main class. In java, code written outside the main function won’t execute.

After write code you have to compile this java program. So save this code file with .java extension.

And remember while saving java file you have to name your file as your main class’s name. For example in our code main class is ‘TextProgram’ and if we save this file as textProgram or Textprogram then it won’t run.

Now open you CMD where you have saved your file and type ‘javac TextProgram.java’ and then press enter.

After successfully compiled a new file is generated with .class extension. This .class file contains byte code version of our java program which only understood by JVM.

After successfully compiled a new file is generated with .class extension. This .class file contains byte code version of our java program which only understood by JVM.

So to run this file type ‘java TextProgram’ in cmd

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.