Java:
Key features of Java:
Platform independent: It can be run on any OS that have JVM.
Setting Up Java
To start coding in Java, follow these steps:
Download & Install JDK (Java Development Kit) from Oracle’s official website.
Set Up Environment Variables (if necessary) to run Java from the command line.
- Choose an IDE: Select the correct IDE for writing code. Example: VS code.
Writing Our First Java Code:
Explanation:
public class HelloWorld: Declares a class namedHelloWorld.public static void main(String[] args): The entry point of a Java program.System.out.println("Hello, World!"): Prints text to the console.
Java Basics:
Data Types & Variables
Java has strong typing, meaning variables must be declared with a type.
int age = 25;
float price = 99.99f;
char grade = 'A';
boolean isJavaFun = true;
String message = "Hello Java!"If-Else Statement:
Syntax of If-Else Statement in Python;
if (age >= 18) { System.out.println("You are an adult.");} else { System.out.println("You are a minor.");}Now Lets talk about how to print the string in java.To print we use the function "System.Out.println()".Instead of writing System.out.println() we can simply use the shortcut "sout+Enter."Lets talk about what is the difference println() and print() of java;In Java system.out.println() is used to print a string and after printing it leaves a line empty itself ,whereas, system.out.print() is used to print the string but it donot leave any line after printing the string.Taking the input in java through the user:
To take the input from the user we use the scanner class. For using this scanner class we need to import the java.util.* package.
import java.util.*;
public class practice{ public static void main(String[]args){ Scanner sc =new Scanner(System.in); Integer age=sc.nextInt(); if (age>18){ System.out.println("Adult"); } else{ System.out.println("Not Adult"); }
}}
Guys now lets talk about Switches.
But before going to switches lets discuss whats the need of it and for that we take one Question.
Question: In the Science Extibition Rahul made a Robot from his interlligence which used to greet everyone in different language on pressing the different buttons.So now Rahul's teacher asked him that what code of java will he use to print the samein VsCode.
Programme:
import java.util.*;
public class practice { public static void main(String args[]) { Scanner sc = new Scanner(System.in);
int button = sc.nextInt();
if (button == 1) { System.out.println("Namaste"); } else if (button == 2) { System.out.println("Bonjour"); } else { System.out.println("Hello"); } sc.close(); }}
As you can conclude now its very hetic and long method, Suppose there are 100's of button and each buttonhave different work so will you write the code in the same manner for all the buttons.
No!! am I right for making our work easy java introduce the Switches Let's discuss them. Switches:
Switches provides us the compactibility to reduce the size of the code and make the interface of code looking good. Switches also known as the case reader function in which it usally takes the caseand print us the suitable outcome.
Syntax of Switches in Java:
Lets take that Question again and create the code with the help of switches.
Programme using switches looks like this;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int button = sc.nextInt();
switch(button){
case 1: System.out.println("Namaste");
break;
case 2: System.out.println("Bonjour");
break;
case 3: System.out.println("Hello");
break;
default:
System.out.println("invalid Number entered or button clicked");
}
}
}
Now you can see that our code is more compact and looking good isn't it.
Now Let's discuss homework Quesions of python.Q1. Print the following pattern:
FGHIJ
GHIJ
HIJ
IJ
J
programme:
for i in range(70,75): for j in range(i,75): print(chr(j),end="") print()
Q2. Print the following pattern:
54321
4321
321
21
1
programme:
for i in range(5): # Print leading spaces print(" " * i, end="")
# Print numbers in decreasing order for j in range(5 - i, 0, -1): print(j, end="")
print() # Move to next line
Q3. Print the following pattern:
54321
4321
321
21
1
Programme:
for i in range(1,6): for j in range(5,i-1,-1): print(j,end="") print()
Q4. Print the following pattern:
1
12
123
1234
12345
programme:
for i in range(1,6): for j in range(1,i+1): print(j,end="") print()
Q5. Print the following pattern:
1
21
321
4321
54321
programme;for i in range(0,5): for j in range(i+1,0,-1): print(j,end="") print()
Q6. Print the following pattern:
554543543254321
programme:
for i in range(5): # Print decreasing numbers for j in range(5 - i, 0, -1): print(j, end="") print() 7. Write the following pattern:
EDCBA
DCBA
CBA
BA
A
programme:
for i in range(5): # Print leading spaces print(" " * i, end="")
# Print numbers in decreasing order for j in range(69 - i, 64, -1): print(chr(j), end="")
print() # Move to next line
8. Write the following pattern:
E
DE
CDE
BCDE
ABCDE
programme:
for i in range(5): print(" " * (4 - i), end="") for j in range(69 - i, 70): print(chr(j), end="") print()
9. Write the following pattern:
A
BA
CBA
DCBA
EDCBA
programme:
for i in range(5): print(" " * (4 - i), end="") for j in range(i, -1, -1): print(chr(65 + j), end="") print()
10. Write the following pattern:
ABCDE
BCDE
CDE
DE
E
programme:
for i in range(5): print(" " * i, end="") for j in range(65 + i, 70): print(chr(j), end="") print()
Now take some more questions.
Question11: Write the code to print the first n prime number.programme:
n = int(input("Enter how many prime numbers you want: "))count = 0num = 2
while count < n: i = 2 is_prime = True while i < num: if num % i == 0: is_prime = False break i += 1 if is_prime: print(num, end=" ") count += 1 num += 1
Question12: Write the programme to print the fibonacci Series.programme:n = int(input("Enter how many Fibonacci numbers you want: "))
a=0b=1count=0while count<n: print(a,end=" ") c=a+b a=b b=c count+=1Question13: Write the programme to print the Geometric progression.programme:a = int(input("Enter the first term: "))r = int(input("Enter the common ratio: "))n = int(input("Enter the number of terms: "))
for i in range(n): term = a * (r ** i) print(term, end=" ")
Question14: Write the programme to print that the string entered by the user is palindrome or not.programme: We will discuss this programme in two ways and we will also discuss new thingfor doing it with second method.Method 1: using indexings=input("enter the string")rev=""for i in range(-1,-len(s)-1,-1): rev=rev+s[i]if s==rev: print("palindrom")else: print("not a palindrom")
Method 2: Silicing:
New topic of python which we study in detail in our next blog for list,string, tuples etc.for now the only thing you have to keep in mind is that we use [::-1] for reversing the string.
s=input("enter the string")if s[::-1]==s: print("palindrome")else: print("not a palindrome")
Let's give you some basic homework question on behalf of today's java blog.
Question: Suppose you are raghav and your teacher to take the name and marks of the studentas a input and find weather he\she is pass or fail.
Question: Suppotose pinky has made a switch board of 8 switches. you have to use the switch function to take the output. the work of each switch can be determine by the coder.
There are not many question regarding this when we procceed to loops in java then we will practice more questions.
Let me give give you more question regarding pattern in python.
Question: Write the code to print the pattern:
11111 2222 333 44 5 Question: Write the code to print the pattern:123456789101112131415Question: Write the code to print the pattern:101101010110101Question: Write the code to print the pattern:1____112___21 [Take _ as a space not the symbol or character]123__3211234_4321123454321
Question: As in previous blog i gave you the code for Geometric progression, now you have to create the code for arthematic and harmonic progression.
Question: Write the code to print starting 100 prime numbers.
Question: Write the code to take the input of number from the user and then you have to check weather the number is prime or not.
Question: This question is may be tough for the beginners but easy to writethe choice based programme in which you will enter your choice and programme works accordingly.
Output should be: Enter the choice:1 x=10 numbers should entered by the user y=20 s=x+y s=30 ✅ Conclusion
That's all for today’s Java basics, my dear friends! 🚀
I hope you now have a solid understanding of how Java works — from
setting up your system to writing your very first Java program, taking
user input, using conditions, and even simplifying code with switch
statements.
As always — practice these concepts, try the homework questions, and
experiment with the programs to build your confidence. 💻✨
👉 If you haven't already checked my Loops in Java – From Basics to Best Practices make sure you do! It will give you a stronger grip on core programming
logic which applies to any language.
In the next blogs, we’ll move ahead with loops in Java, where we’ll solve
even more interesting problems and pattern questions together.
Stay tuned, and keep coding with passion! 🔥


4 Comments
Very helpful content
ReplyDeleteThanks bro
DeleteGood tutorial 🔥🔥🔥
ReplyDeleteInsightful
ReplyDelete