Day 2: Basics of Java

Day 2: Basics of Java

This is purely a guidance and I recommend everyone following this series to practice the code that I share.

Short definition of various Java basic concepts

  • Variables: Variables are used to store values that can change during program execution. In Java, you must declare the type of a variable before using it.
  • Data types: Data types define the type of value that can be stored in a variable. Java has several primitive data types, such as int, float, and boolean, as well as reference types, such as String and Object.
  • Operators: Operators are used to perform operations on variables and values. Java has several types of operators, including arithmetic, comparison, logical, and bitwise operators.
  • Loops: Loops are used to repeat a block of code a certain number of times or until a certain condition is met. Java has three types of loops: for loops, while loops, and do-while loops.
  • Conditions: Conditions are used to perform different actions based on whether a certain condition is true or false. Java has several types of conditional statements, including if-else statements, switch statements, and ternary operators.

Here are some resources you can use to learn more about these topics:

These resources are part of the official Java tutorials provided by Oracle. They are comprehensive and provide examples to help you understand these concepts better. Additionally, there are many other online resources available, such as blogs and YouTube videos, that can provide more in-depth explanations and examples of these topics.

YouTube Channels:

Blogs and Websites:

These resources offer a range of tutorials, guides, and examples on Java programming, including variables, data types, operators, loops, and conditions. They also cover more advanced topics, such as object-oriented programming, multithreading, and web development using Java frameworks.

I would recommend personally to follow Baeldung and GeekforGeek for any topic in Java. Java point and guru99 are also wonderful sites, but for guru99 the UI is a bit difficult to follow.

But if you are so passionate about programming, MITs Programming for everybody is a wonderful course on all basics correctly covered. Though it covers mostly Python, I have seen many experts recommend this course for basics of programming.

I have also taken it one point of time and watched the videos that are relevant to what I want to learn. One such topic was loops, taken by the Mark Zukerberg himself, if I am not wrong and it made me realize how important it is to be strong in the basics of programming.

If you don't want to waste time reading and watching, go to w3schools https://www.w3schools.com/java/ and start practicing right away without any dependency on setting up Java or IntelliJ in your machine.

But, I highly recommend setting things up in the start to make it easier for you to follow the topics that I would cover.




Here are few simple Java programs for practicing

Adding 2 numbers:

public class AddTwoNumbers {
??public static void main(String[] args) {
????int num1 = 5, num2 = 10, sum;
????sum = num1 + num2;
????System.out.println("Sum of " + num1 + " and " + num2 + " is " + sum);
??}
}



Average of array of numbers

public class AverageOfArray {
??public static void main(String[] args) {
????double[] numbers = { 2.5, 3.5, 4.0, 5.5, 6.0 };
????double sum = 0;
????for (int i = 0; i < numbers.length; i++) {
??????sum += numbers[i];
????}
????double average = sum / numbers.length;
????System.out.println("Average of the numbers is " + average);
??}
}



Check if the number is even or Odd

public class EvenOdd {
??public static void main(String[] args) {
????int num = 4;
????if (num % 2 == 0) {
??????System.out.println(num + " is even");
????} else {
??????System.out.println(num + " is odd");
????}
??}
}



Finding the maximum in an array of integers

public class MaxElementInArray {
??public static void main(String[] args) {
????int[] numbers = { 5, 10, 15, 20, 25 };
????int max = numbers[0];
????for (int i = 1; i < numbers.length; i++) {
??????if (numbers[i] > max) {
????????max = numbers[i];
??????}
????}
????System.out.println("Maximum element in the array is " + max);
??}
}



Fibonacci Series

public class FibonacciSeries {
??public static void main(String[] args) {
????int n = 10, t1 = 0, t2 = 1;
????System.out.print("Fibonacci series of " + n + " terms: ");
????for (int i = 1; i <= n; ++i) {
??????System.out.print(t1 + " ");
??????int sum = t1 + t2;
??????t1 = t2;
??????t2 = sum;
????}
??}
}



Apart from these programs, try practicing Factorial of a number, Number is prime or Not, Reverse a String, Sum of first n natural numbers and Bubble sort algorithm.








要查看或添加评论,请登录

Shree Krishna Priya J的更多文章

社区洞察

其他会员也浏览了