
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.
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.
| 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 } |
| Basic of Programming Java |
| String name; name = “PWSkills”; OR String name = “PW Skills”; |
| 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 |
| // This is a single-line comment Code |
| /* Comment Start —---- —---- —- Comment ends */ |
| for (int i = 0; i < 5; i++) { // code } |
| while (condition) { // code } |
| do { // code to be executed at least once, then repeat while the condition is true } while (condition); |
| // 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. } } |