This Tutorial site will help to learn Java ,JavaScript, Computer Science for beginners. we also covered bonus topics like Finance, Health and Hindu | Indian boys and girls name .
Switch statement is alternative and more cleaner way to write code if we have more than two options. we can use else if, this is better way. So lets start learning.
If you want to watch video tutorial for how to use switch statement in java simple click on below tutorial link.
Java Tutorial Switch Statement in Hindi
For illustration purpose we will use school grade example . we will male program if student get grade A we will print wow great job, if student get grade B we will print good job, if user get grade c we will print you passed, if user get grade f we will print , you failed and for any other grade we will print sorry you not appear . So lets get started.
First we will declare variable grade, datatype as char.
After that we will type switch and in the parenthesis we will pass our grade. and we will add curly brace. Now we will type case A and will print great job, and add break. It means in the grade if user get A grade then we will print this statement. Now we will add case B and we will print good job . and we will type break . Now we will repeat for grade C. Repeat process for grade F and add print statement. now at last we will add default it means any thing which is not mentioned in case , will fall in default and run that block of code.
Now in the grade variable assign A as grade and run program. In the result it will run block of Case A similarly try different Cases and check result.
If there is two condition we use if else but if condition is more than one? well we can use else if. So we will learn how else if condition is used in java with example. If you want to watch video tutorial about how to use else if in Java , below videos are available in English and in Hindi languages.
Here is scenario
Write Java program of where if food is warm print out food is perfect , if food is cold print "could you warm please" and for any other option print "I will wait"
Java Else if video tutorial in English
Java Else IF Statement Video Tutorial in Hindi
Now lets create class,
As we can observe in code we have created class name ElseIF and inside this class we created method name myElseIfMethod . We do not want to return but just would like to print as a result we are using void. In side curly brace we want to write our logic .
Now lets create variable and data type is string and name of variable is food and we set to warm for now . we will change it later to see how it will be in output.
Now we will use if condition so we use if and inside bracket we will specify our condition . In last tutorial we used double equal sign to compare and in this tutorial we are using dot equal to as shown in the code. we are comparing if food is warm then , system will run that block of code which is food is perfect.
But if food is cold then? we will write different logic for that . Now we will type else if and again in bracket we are specifying condition that if food is cold then we will print out please warm food
And other than that , we will print out I will wait . This is how if we have more than two condition we can use else if condition. You can change variable to cold , warm and other and check out put .
Code
public class ElseIF {
public void myElseIFMethod(){
String food= "warm";
if(food.equals("warm")){
System.out.println("Food is perfect");
}
else if(food.equals("cold")){
System.out.println("could you warm it please..");
}
else{
System.out.println("I will wait .....");
}
}
Now lets create object in main method . To create object on main method first type name of class then create provide name of variable and then equal sign , new and once again name of class and bracket. If you type variable and dot it will provide you accessible methods , select method which we created, run and check in the output.
If you want to learn more step by step java tutorial for beginners here is link .
In this tutorial we will learn how to use Arithmetic Operators, Mathematical operations in Java. we will create separate non static methods for each operation like addition, subtraction, multiplication, and division and we call those in main method. If you want to follow along by watching below is link for java basic calculator where we have used arithmetic operation by creating method. Step be Step tutorial is in English and Hindi Language.
Java Basic Calculator Video Tutorial in English
Java Basic Calculator Video Tutorial in Hindi
For more video click on this link @amazingcodingtechworld
First create class and checkmark on public static void, so we can have main method .
Create method, in this example we created method Add and we use void since we are not returning anything. After method name we use parenthesis and then curly brace where we use our logic inside. we created variable named a and datatype is integer and hold value of 5. After that we have created another variable b which datatype is integer and hold value of 10. Since we are doing Addition we need result of both number that is why created another variable named result and we adding both number and we are printing it that is why we are using system.out.println as described in below image.
Now to call method in main class create instance of class . since we are using not static method we have to create instance of class. Syntax is name of class then variable then we use new word and class name then parenthesis. now if you use dot operator after that variable we will get all method which we can able to access. we want to use Add method so lets select it and check in output. we can observe we have result.
Now lets do Subtraction by creating non static method, we have created method and created variable , a, b, and result . All have data type as integer and we are calling in main methods.
Now lets try out multiplication everything is same but in the result variable we have just changed operation.
Now we are doing last operation, division. so we are doing same thing created three variables and calling in the result we have changed operation we are doing division and we are calling method in main .
In this java tutorial we will learn about how to create method and call method with example step by step . If you have any doubts you can watch java tutorial which is in English and Hindi languages.
Create method and call in Java in English Video Tutorial
Create method and call in Java in Hindi Video Tutorial
For more video click on this link @amazingcodingtechworld
Lets create class and and check mark on public static void so it will create main method, where we will call .
public class JavaMethod {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Now will create method in side class called Java method.
we have created myMethod and we use void since we are not returning anything and when we create method we give parenthesis() at will provide curly bracket . Inside out curly bracket we have out logic in this case we want to print "hello.....my method"
now to call this , in the main method we have to create instance of our class. so syntax is name of our class then provide variable name which is instance of our class and = new and provide class name. Now we have instance of our class now we can use it. if we type that instance and use dot operator we can access method inside our class and we can see out put as described in picture. This is how you use to create and call non static method. If you want to create static method you simple type name of method in main class.
In last tutorial we learn how to create class in eclipse and print hello world program. Now in this tutorial we will learn Java Data type step by step by practical example.
Like other languages java has different data type, context is same but there may be change in syntax and we will learn them one by one.
Lets create class, It the src folder right click and click on new provide name in this example we give name as datatype . checkmark on public static void main so it i will create main method in our class. and click on Finish.
Now check in editor , we can observe we have class name data Type and we have main method , we can ignore comment or remove it. If you want to learn by watching here is link of step be step video tutorial data type Java program for beginners in Hindi and English language. Please click on link.
Java Data Type Tutorial Video in English
Java Data Type Tutorial Video in Hindi
Data Type:
java Integer
In java we have to let system know what type of data type are we using. Lets start with int. we create variable , which hold data , so we will type int and we created variable name a and we store value of 5. so what is cool about variable every time we change value we do not have to change else where , we just use variable because it holds value. As we observe in below picture what would be output in console? you get it . it's 5 . click on green error button (Run) . but before that make sure you have saved it.
Console out put
Now what if you change a =10 and rerun program, what would be output? yes it would be 10.
Java Double
Now lets create another variable b , and set datatype integer and assign value as 1.5 (int b = 1.5) . Hmmm now we get error message now either we have to cast or we have to change type now change datatype as double instead of int , now you can see error is gone . it means if number is not whole we like 1.5 we should use double . now run program you will get out put as shown in below image
Java Character
Now it is correct time to introduce char, what would be best example? if teacher gives score to student like a, b, c ,d, f. we can use data type as char as describe in example , save it and Run.
Java String
If we would like to use sentence and would like to print it what datatype should we use? Answer is String lets see how to use string in java
we will type String as data type and we create variable which hold some kind of value and we assign as = sign. but unlike char we will use double quotation in string while in char single. In below example we created variable name and type of string which hold value(John) now save , run and observe in console. we get our correct value.
Java Boolean
Now we will learn Boolean . Boolean is true or false. And we can use boolean to declare value. In this example we have set boolean as true and we printed in console.
Why we have so many data types?
well these are not only the type there are byte, short, long, float etc . All have different size and capacity. If it is byte it would be size of 1 byte and can store whole number from -127 to 127. If we use short it will take 2 byte and we can store whole number from -32768 to 32767. If we use in as data type it will take 4 byte and can store number -2147483648 to 2147483647, for long it will take 8bytes and can store value from whole number -9223 372 036 854 775 808 to 9223 372 036 854 775 807.Now you can understand we can use same datatype for multiple thing but it will take more memory , that is why it is important to use wisely .
hope you like this tutorial if you would like to know more, please click on below link.
If you are new to any language we start with Hello World which is first step of learning .
If you have any drought or confusion there is step by step video tutorial in Hindi as well as in English about to start to learning Java by creating simple program of Hello World .
Pre-condition is first download code editor , like Eclipse https://www.eclipse.org/downloads/ . which will ease your work. Also you have to download Java based on your computer system. https://www.oracle.com/java/
Make sure you set Environmental variable . Or open command prompt by typing cmd in search bar. now type javac if you have few option as below it means you have set up correctly. there are lots of video about how to set up so you can follow that.
Video In English
Video In Hindi
open Eclipse or any code editor and click on File then Click on New
Now try to find java project if you can not find then select other project and type java and click on Java project , click on next
Enter name of Project and click on Finish
Now expand new created project and Right click on SRC, then New and Class.
Enter class name , and check mark on public static main and click on Finish.
Since we have check mark on Public static main system will create class with main method .
Now for print in console in Java we will type system.out.println and in brace we will type hello world since it is string will use string and semicolon after code which is compulsory in java. Make sure we will save , use control + S .
Now Click on Run and select First from drop down which is out class name if you have given something else make sure you select that name now check console.
Congratulation we have our first program of Hello World.
Java programs for Beginner- If you are beginner in Java and want to learn java with Fun way here are some projects/ java problem for beginner. You have given java problem, Solution with explanation and we have added solution video. Looping Java Problem 1. Print 1 to 100 by using For and While Loop. Solution: we have to to break down small parts. Here are three things Printing and its solution is System.out.println, starting point which is 1 and Ending point which is 100. And we have to add number by 1 . Code public class LoopC { public static void main(String[] args) { //Print 1 to 100 ------ // ----------------------For Loop------------------- for(int i=1;i<=100;i=i+1){ System.out.print(i); } // --- ---------------------with while loop----------------------- int i= 1; while(i<=100){ System.out.println(i); i++; } } } Solution Video For Problem 1
2. Print 1 to 20 Odd Number by using For and While Loop. Solution: Since we are printing number from 1 to 20 , out starting number will be 1 and Ending would be 20. Only new thing is odd number so will use i=i+2 instead of i=i+1. see below code. Code public class LoopC { public static void main(String[] args) { //-------Print 1 to 20 Even Number -----
// --------------------------For Loop----------------------- for(int i=1;i<20;i=i+2){ System.out.println("This is odd "+i);
} // ------------------While loop---------------------- int i= 1; while(i<20){ System.out.println("odd number "+i); i=i+2; } } } Solution video
3. Print 1 to 20 Even Number by using For and While Loop. Solution: Since we are printing number from 1 to 20 , out starting number will be 1 and Ending would be 20. Since we want Even number we will start with 2, and will use i=i+2 . public class LoopC { public static void main(String[] args) { // TODO //Print 1 to 20 Even Number //----------------For Loop for(int i =2;i<=20;i=i+2){ System.out.println("my number is Even "+i); } // -------While Loop int i = 2; while (i<=20){ System.out.println("my number is Even "+i); i=i+2; }
4. Print 1 to 10 in Descending order. For Ex- 10, 9, 8.....1
Solution : Since it is Descending order Our starting point is 10 and Ending point will 1. Also due to Descending order , decr. will be i--(which is same as i = i-1)
Code
public class LoopC { public static void main(String[] args) { //Print 1 to 10 in Descending order. For Ex- 10, 9, 8.....1 //-------------------For Loop for(int i=10;i>=1;i=i-1){
System.out.println("this is Des "+i); } // --------------While Loop int i= 10; while(i>=1){ System.out.println("this is Des "+i); i--; } }
5. Print Sum of 1 to 10 Solution: For print 1 to 10 as we know how to print it. but if we want to add sum of all number . we declare sum as variable as zero, now as soon as I will increase we will add to sum and it will loop or repeat untill it will reach to 10; Code -------------------------------------------------------------------------------------------------------------------------
public class LoopC {
public static void main(String[] args) {
//---------------For Loop int sum= 0; for(int i = 1;i<=10;i++){ sum= sum+i; System.out.println("Sum is "+sum); }
//-----------------While Loop int wsum= 0; int i =1; while(i<=10){ wsum=wsum+i; System.out.println("Sum is "+wsum); i++; } }
}
Solution video
---------------------------------------------------------------------------------------------------------------------- 6.---------Print Table of 5 by using For loop and while loop Solution :; Here we are printing table of 5 so we declare variable and store 5 in it. rest of is easy, we loop through as multiply 5 by i ,soevery time i , increase we are doing multiplication . Code public class LoopC { public static void main(String[] args) {
int multi= 5; for(int i= 1;i<=10;i++){ System.out.println("5 *"+ i +" = "+multi*i); } int mul= 5; int i = 1; while(i<=10){ System.out.println("5 *"+ i +" = "+mul*i); i++; } } } Solution video
---------------------------------------------------------------------------------------------------------------------
7. Ask name from User , print it , Ask Question to user, take input from user , validate and print
Solution: We ask question by System..out.println("What is your name?"),For user input we need scanner class also we have to import class. After taking value from user we store in variable and print it. we ask second question and again we take answer from user , and that answer we validate and provide response accordingly, Please see below code, and for more info please watch solution video.
Code
import java.util.Scanner;
public class Input { void newInput(){ System.out.println("What is your name?"); Scanner s = new Scanner(System.in); String ns= s.next(); System.out.println("Hello "+ns); System.out.println("Longest River in the world?"); String ans= s.next(); System.out.println("Your answer "+ans); if(ans.equals("Nile")){ System.out.println("Good Job!!! "); } else { System.out.println("Sorry your answer is not correct "); } }
public static void main(String[] args) { Input in= new Input(); in.newInput();
}
}
Solution Video
------------------------------------------------------------------------------------------------------------------
8. Given Array Find Number is Positive , Negative Number
here you need for loop and use if to check condition in loop
Code
public class PoNe { //Given Array Find Number is Positive , Negative void myArray(){ int [] num = new int[3]; num[0]=10; num[1]=0; num[2]=-10; for(int i = 0;i<3;i++){ if(num[i]>0){ System.out.println(num[i]+" is Positve"); } else if(num[i]<0){ System.out.println(num[i]+" is Negative"); } else { System.out.println(num[i]+" is Zero"); } } }
public static void main(String[] args) { PoNe p = new PoNe(); p.myArray();
}
}
9. How to print all the odd number in descending order?
First printnumber in decending order, then add if statement and add odd number condition
public class Number {
void oddNumberDescendingOrder(){
for(int i = 10; i>=1;i--){
if(i%2==1){
System.out.println(i);
}
}
}
public static void main(String[] args) {
Number n= new Number();
n.oddNumberDescendingOrder();
}
}