Learning the basics of programming Java is crucial before starting with competitive programming. If you are learning Java programming, then it is important to be familiar with its fundamentals.
You might be thinking, What are the basics of Java programming? The basics include syntax, data types, operators, keywords, libraries, and other concepts to start your coding journey with Java. Read this basic post on programming Java to clear your concepts before starting with Java programming.
Why is Java everybody’s favourite?
Java is a highly popular object-oriented programming language with multi-platform support. It is a fast and highly reliable language used for developing various big data applications and back-end tasks. Java programming language is easy to write, compile, debug and learn as compared to other programming languages.
Major companies are in search of skilled Java programmers to execute their various application development tasks. Java developers are always in high demand because of the efficiency and trust this language has created in the tech market.
Highlights:
- Importance of Learning Java Basics: The content underscores the significance of mastering the fundamentals of Java programming before delving into more advanced topics or competitive programming. By emphasizing the importance of understanding Java syntax, data types, operators, keywords, and libraries, readers are encouraged to establish a strong foundation for their coding journey with Java.
- Popularity and Utility of Java: Java’s widespread popularity and utility are highlighted, portraying it as a highly favored object-oriented programming language with multi-platform support. Its reputation for speed, reliability, and ease of use makes it a preferred choice for developing various applications, including those involving big data processing and backend operations. The content acknowledges Java’s role in powering critical systems and its continuous demand in the tech industry.
- Exploration of Java Data Types: Readers are introduced to the fundamental concept of data types in Java, encompassing both primitive and secondary data types. The distinction between primitive data types (e.g., int, float, char) and secondary data types (e.g., arrays, classes) is elucidated, providing a clear understanding of how Java handles different kinds of data.
- Comprehensive Overview of Java Operators: The content offers a comprehensive overview of Java operators, covering arithmetic, assignment, relational, logical, and increment/decrement operators. Through detailed explanations and examples, readers gain insights into how each operator functions and its practical applications in Java programming, facilitating effective manipulation and computation of data.
- Introduction to Classes and Objects in Java: The concept of classes and objects in Java is introduced, highlighting Java’s object-oriented paradigm. Readers learn how classes serve as blueprints for creating objects, which encapsulate both data (attributes) and behavior (methods). Through a clear example demonstrating the creation of a Person class with attributes like name and age, readers grasp the essential principles of object-oriented programming in Java.
Basics of Programming Java: Data Types
Java programs are easy to learn and implement. It supports all primitive and secondary data types, such as integer, float, double, character boolean, etc. Also, as Java is an Object Oriented Programming language, it supports objects and classes.
There are two major classifications of data types in Java:
- Primitive Data Type: It consists of primitive data types such as int, float, char, double, boolean, short, byte, etc.
- Secondary Data Type: The secondary data types consist of arrays, classes, modifiers, class names, etc.
Let us check an example of Java consisting of major data types.
public class car //class data type is a non-primitve data type in Java
{ Public: char color; // char and double are primitive data type double model; Public void gear (); // secondary data type } |
Basics of Programming Java: Declaring and initializing Variables
We can initialize and declare a variable at the same time. Also, we can first declare the variable and initialise it at a later point. Check the process of declaring and initializing variables in Java.
Basic of Programming Java |
String name;
name = “PWSkills”; OR String name = “PW Skills”; |
We can declare a constant using the ‘final’ keyword in Java, making the variable constant and immutable.
Basics of Programming Java: Java Environment
Java consists of the three major environments given below.
- JDK (Java Development Kit): It is a development environment that consists of Java Runtime Environment (JRE), interpreter, compiler, archiver, Javadoc, and other tools required for developing Java applications and applets.
- JRE (Java Runtime Environment): The JRE is an environment that is used to run our program. It provides various requirements for executing our Java code and consists of supporting files and documentation.
- JVM (Java Virtual Machine): JVM is responsible for executing the Java program code line by line. It is also known as an interpreter. JVM makes Java a platform-independent language.
Basics of Programming Java: Operators in Java
Java support various operators to provide a high degree of operations on different values. Some of the commonly used operators in Java are mentioned below.
Basic of Programming in Java: Operators in Java | ||
Arithmetic Operator in Java | ||
Operator | Description | Example |
+(Addition) | It is used to add two or more values. | 15 + 10 = 25 |
-(Subtraction) | It is used to subtract two or more values. | 15–10 = 5 |
*(Multiplication) | It is used to multiply two values on either side of the operator. | 15*10 = 150 |
/(Division) | It is used to divide the left hand operator by right hand operator | 15/3 = 5 |
%(Modulus) | It returns the remainder when the left-hand operator is divided with the right-hand operator. | 12%3 = 0 |
Assignment Operator in Java | ||
= | It assigns the value to a left-side operand from a right-side operand. | c = 15, c stores the value 15. |
+= | It keeps on adding the right operator to the left operand and assigning the result obtained to the left operand. | a +=b; |
-= | It subtracts the right-hand operand from the left-hand operator and assigns the value to the left operator. | a -=b is the same as
a = a-b |
*= | It is used to multiply the value in the right hand to the left hand operand. | a *=b is the same as
a = a*b |
/= | It takes the division of the two operands and then assigns the result to the left operand. | a /=b |
%= | It takes the modules and assigns the remainder value at the left-hand operand. | a %= b, it is same as a= a%b |
^= | It is used to perform exponential calculation and assign the calculated value to the left hand operand. | a^=b |
Relational Operators in Java | ||
== | It checks whether the left-hand and right-hand operands are equal. | 14 == 21 is not true. |
!= | It checks whether the left-hand and right-hand operands are not equal. | 14!= 21 is true |
> | If the value of the left operand is greater than the right hand operand, then it returns true. | 12 > 6 is true |
< | If the value on the left-hand side is smaller than the right-hand side, then it returns true. | 6<5 is false |
>= | It tells the left side value is greater or equal to the right-hand side. | 9>=9 is true
10>=8 is true |
<= | It tells the left value is smaller or equal to the right-hand side. | 6<=9 is true |
Logical Operators in Java | ||
&& (and) | It will return true only if both the operands are true. | a = 4
a<9 && a<10 will return true. |
|| (OR) | It will return true if any of the operands on either side are true. | a = 11
a<10 || a<12 |
! (NOT) | It returns true when both the operands are false. It is also used to negate the value of a variable. | a = true
!a, it will return false |
++ | It increments the value of a variable by 1. | a = 1
a++ Now, it will return a = 2 |
– | It will decrement the value of a variable by 1 | a = 5
a– Now, it will return a = 4 |
Basic of Programming Java: How to add Comments in Java Code
Comments are very helpful in making code more readable and explaining the details. It can help you explain some major logic implemented simultaneously in the code area. It will be useful when you are coming for a quick revision or while debugging code for errors. Comments are not executed during compilation.
In Java, you can have two major types of comments, single-line comments and multi-line comments.
- Single Line Comments: It is used while commenting on a single line for describing a code area. The syntax for single-line comments in Java is “//”
// This is a single-line comment
Code |
- Multi-Line Comment: When comments are long, then we need to use multi-line comments to enclose the comment statement inside.
/* Comment Start
——- ——- —- Comment ends */ |
Basic of Programming in Java: Loops
Java provides many loop options for iterating through data. Check some of the methods below.
- for loops: The ‘for’ loops are used to iterate through elements one by one. It takes the starting value and end value with gradually incrementing the pointer.
for (int i = 0; i < 5; i++) {
// code } |
- While loop: The while loop is used to run a statement inside until the given condition is true.
while (condition) {
// code } |
- do-while loop: This loop is used when we need to execute our statement at least one time. We will repeat the while loop until the conditions are true.
do {
// code to be executed at least once, then repeat while the condition is true } while (condition); |
Basics of Programming Java: Classes and Objects
Java is an object-oriented programming language that consists of classes and objects. Objects are the instance of a class and Classes are blueprints used for creating objects. It consists of data and methods to perform various functions. Let us understand through an example. Given here is a class of Person consisting of attributes like name and age. It also consists of method (functions).
// Defining a class named “Person”
public class Person { // Fields (attributes) String name; int age; // Methods (behavior) public void speak() { System.out.println(“Hello, my name is ” + name + ” and I am ” + age + ” years old.”); } } public class Main { public static void main(String[] args) { Person person1 = new Person(); // Object in Java Person person2 = new Person(); // Setting values for the fields person1.name = “John”; person1.age = 30; person2.name = “Jane”; person2.age = 25; // Calling methods on the objects person1.speak(); // Output: Hello, my name is John and I am 30 years old. person2.speak(); // Output: Hello, my name is Jane and I am 25 years old. } } |
Learn Java with PW Skills
Learn Java along with data structure with our all-in-one course, Decode Java with DSA. Start your career as a Java Developer or Software developer with our course. Get 100% placement assistance and an industry-recognized course completion certificate after completing the course. Learn more only at @pwskills.com.