This Tutorial Provides an Introduction to the Basics of Java Coding Language. We will Introduce Topics Such as Java Class, OOPS, Syntax, and Core Java Concepts:
In our previous tutorials, we have discussed the characteristics and features of the Java language. We have also learned about Java applications. We got acquainted with download, installation, and various Java IDEs that we can use to develop programs and applications in Java.
What You Will Learn:
Basics Of Java Programming
In the last tutorial, we have discussed our first Java program “Hello, World”. In that, we learned the basic syntax that we need to just display a message “Hello, World”. But just learning to display a message is not enough.
If you want to master the language, you need to first understand the basic syntax and semantic rules for the language, along with the various constructs, mathematical operations as well as repetitive programming constructs.
You also need to be aware of various object-oriented programming features of Java, data structures, and data types that Java uses. When you think you have mastered all these among various other features that Java offers, then you can call yourself proficient in Java language.
In this tutorial, we will introduce the basics of all the programming topics that we are going to cover in this entire Java tutorial series.
Let’s get started with the Java syntax!!
Every programming language has certain rules and regulations that a programmer needs to follow while writing programs. The respective language compiler checks your program for syntax rules and validation. Java also has lots of constructs and components that make it easier for programmers to write quality programs.
The “Hello, World” program in the previous tutorial gave you an idea about the basic structure of a Java program in detail. Now let's go to the other constructs/components that a Java program will include.
Given below are some of the conventions that a Java programmer needs to follow while programming in Java.
(i) Class names: In Java, the first letter of class name for every class should be uppercase. For example, a class salary will be named as per the convention as “Salary”.
If you have a class name that is combined using more than one word then each letter of the first word will be an uppercase letter.
For example, class MyHelloWorld.
(ii) Method name: All method names in Java start with a lowercase letter. If the method name comprises more than one word, then the first letter of each of these inner words will be uppercase.
Example: display (), myMethod () => these are valid method names in Java.
(iii) Program file name: The filename of the Java program should be the same as the name of the public class with an extension “.java”.
For example, if the public class name is “MyFirstClass”, then you save this code in the file named “MyFirstClass.java”.
Note that a java source code may not have any public class. In that case, you can have your name for the source file.
(iv) Case sensitive: Java programming language is case-sensitive. This means ‘Hi’ and ‘hi’ are two different parameters.
(v) Main method: The method ‘main’ is the starting point of execution and is a compulsory method in all Java programs.
Identifiers are the names given to various program components like methods, classes, variables, objects, etc.
A Java programmer has to follow the below rules for identifiers:
- An identifier should always start with letters (A-Z/a-z) or an underscore character (_) or currency character ($).
- The identifier cannot have the same name as a Java reserved Keyword.
- Beyond the first character, an identifier can have any combination of characters.
- In Java, like other language syntax, identifiers are also case-sensitive.
Hence as per the above rules, the following identifiers are valid.
myVar, _salary, $sum
The following identifiers are not valid.
123var, *mult, int.
Modifiers change the accessibility of variables, methods, etc.
There are two types of modifiers in Java:
- Access modifiers: There are four access modifiers in Java namely, public, protected, private, and default. These are used to define accessibility for packages, classes, class members, etc.
- Non-access modifiers: Java supports non-access modifiers namely – final, abstract, and strictfp. These are mainly used to define inheritance, polymorphism style, etc.
Any variable that holds the value needs to have a type of the value which it is going to hold i.e. whether a variable is going to hold a numeric or string or character. This is called the data type of the variable.
Java has various data types as depicted in the below diagram.
We will discuss each of these data types in separate tutorials.
Java supports the following three types of variables:
- Class or static variables: This type of variable can be accessed without an object.
- Non-static or instance variables: These variables are member variables which are accessed with the help of a class object.
- Local variables: Local variables are local to a particular block of code and cease to exist out of this block.
There are certain words reserved in Java language for its own use and cannot be used as variable or identifier names.
The following table gives the list of these words known as “Keywords”.
Comments are the statements that are ignored by the compiler. You can provide comments for your code to make the code more readable and easy to understand.
Java supports three types of comments:
- Single line comments denoted by ‘//’
- Multi-line comments represented by ‘/*… */’
- Documentation comments denoted by ‘/** ******/
You will learn more about these comments in our subsequent tutorials.
Operators are symbols that perform logical and mathematical operations on variables or identifiers. These variables or identifiers are called Operands.
Java supports various operators as shown in the below diagram:
More about Operators in Java will be discussed in the later tutorials.
Also called as control statements. These statements change or control the program execution based on a particular condition. If the condition is true, a block of code following this condition is executed, else a different block is executed.
Java has the following control/decision making statements.
In programming languages, looping is included to repeatedly execute a block of code. The looping usually starts with a test and the block of code is executed repeatedly for a fixed number of times called iterations or till a condition is fulfilled.
In Java, you have the following loop constructs.
We will learn loops in detail in our subsequent tutorials.
Arrays are nothing but a data structure that is used to hold the data elements of the same type sequentially.
Java arrays are also similar to arrays in C/C++ and other programming languages.
- Single-dimensional arrays: A sequence of elements of the same type and can be accessed using an array name.
- Multi-dimensional array: Elements are arranged in the form of rows and columns i.e. in a matrix form.
Java Class & OOPS
A class is a blueprint of any real-life entity, for example, a car. A class in Java consists of data variables and the methods or functions that operate on these data.
Data variables or member variables and methods depict the behavior of objects which are instances of the class. This means the state of the entity represented by a class at any given instant is defined by an object.
An interface in Java is a collection of method signatures and fields. An interface does not have an implementation of methods. A class can inherit from the interface and then implement the interface methods.
Classes and interfaces that have similar functionality or dependency are grouped to form a package. The package makes modularization of code easier in Java.
Frequently Asked Questions
Q #1) What is core Java?
Answer: Core Java is the basic concept in Java-like variables, data types, arrays, OOPS, etc. You will see the overview of the entire core Java concepts here. Core Java is usually a part of the Java SE edition that allows you to develop general Java applications.
Q #2) What are the types of Java programs?
Answer: Using Java, you can develop the following applications.
- Java Application: Executed on the client computer
- Java Applet: Runs in a web browser
- Java Swing application: Application with a GUI
- JAR (Java Archive): Packaged application
- Servlet: Executes on a web server
- EJB (Enterprise Java Beans): Used to develop websites and runs on a web server.
In this tutorial, we saw a brief overview of each of the topics we classify as Java basics. We will cover each topic in detail separately in our subsequent tutorials in this series.
Along with the above topics, we will also cover advanced topics like collection framework, exception handling, multithreading, etc. among other things.