- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Get A Website Plus a Free Domain Name in Just 1 Hour!
Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!
Java Programming for Beginners: A Tutorial and guide of the beginners on Java Programming
Based on the comments I have got, I realized that my previous hubs are not enough to guide beginners on their learning about the language. So I decided to make another hub on what should a real beginner must do. May this hub would help the beginners on their path to Java language.
The following are the most basic that a beginner must learn
1. Data types and Declaration of variables
What are the data types on Java and how to use it on the declaration of variables?
The most commonly used data types in Java are int for integers, double for decimal numbers, char for a character, string for group of charaters and Boolean for true or false variable. Data
See Also:
types are used to declare variables so that a certain a variable can be used to hold string of characters or numbers. You can declare any variable name you want, however, the variable must describe what it should meant to be. For example, num1 is a variable that will hold the first input number and num2 for the second number.
How to declare a variable? The syntax is this,
DataType variableName;
Example:
int num1;
int num2;
Java Source Code Example:
package declationofvariable; public class Main { public static void main(String[] args) { int num1; int num2; double num3; String name; } }
Related Hubs
2. Input and Output in Java
Knowing the input and output in Java is essential on your learning about the language. Before we know about how to input in Java, we must know how to output first the characters or strings in Java.
There is two common ways on how to output in Java:
1.Using System.out.print () – For single line output.
2.Using System.out.println() – For new line output.
Java Source Code Example 1:
package javasample; public class Main { public static void main(String[] args) { System.out.println("Hi! This Is It How It Works!!!"); System.out.print("Is it exciting? :)"); } }
To input in Java you must declare an object using the Scanner pre-defined function. Object is what you call to access any function in Java. Pre-defined functions are those built in functions of the language. You can name your own Object as well as you can also make your own function.
Java Source Code Example2:
package javasample; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("This Is How to Input in an integer and string in Java"); int num1; //declare variable to hold first input String string = " "; // variable for sting input Scanner input = new Scanner(System.in); System.out.print("Please Enter Your Name: "); string = input.next();// input for string System.out.print("Please Enter a Number: "); num1 = input.nextInt(); // input for integer System.out.println(string + " You enter a number " + num1 + " :D"); } }
3. Java Operators
There are 4 kinds of operators in Java, the arithmetic operators, assignment operators, comparison operators and logical operators.
Arithmetic Operators:
+ - Add - - Subtract * - Multiply / - Divide % - Remainder for Division
Assignment Operators:
Assignment Operators Example Elaboration = X=Y X is equal to Y *= X*=Y X=X*Y += X+=Y X=X+Y /= X/=Y X=X/Y -= X-=Y X=X-Y %= X%=Y X=X%Y
Comparison Operators:
== equal for comparison != not equal > greater than < less than >= greater than equal <= less than equal
Logical Operators:
&& and ! not || or
4. If-else Statement
If and else statement is use to execute a code that has 2 or more conditions.
Syntax:
if(true condition here) { If true Code here } else { If false Code here }
Java Source Code Example3:
package javasample; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("This Is How to Input an integer and string in Java"); int num1; //declare variable to hold first input String string = " "; // variable for sting input Scanner input = new Scanner(System.in); System.out.print("Please Enter Your Name: "); string = input.next();// input for string System.out.print("Please Enter a Number: "); num1 = input.nextInt(); // input for integer if(num1 < 500) { System.out.println(string + " You enter a number that is less than 500 :D"); } else { System.out.println(string + " You enter a number that is greater than 500 :D"); } } }
Sample Output:
5. Looping
Looping is use to execute a code multiple times. Loop can be finite or infinite. There are 3 kinds of looping in Java. The for loop, do-while loop and while loop.
For Loop Syntax:
for(initialization; condition; increment ) { Codes here }
For Loop Example:
package javasample; import java.util.Scanner; public class Main { public static void main(String[] args) { int num1; //declare variable to hold first input String string = " "; // variable for sting input Scanner input = new Scanner(System.in); System.out.print("Please Enter Your Name: "); string = input.next();// input for string System.out.print("Please Enter a Number: "); num1 = input.nextInt(); // input for integer System.out.println(string + " the even numbers of number " + num1 + " are:"); for(int i = 1; i <= num1; i++) { int x = 1; System.out.print((i+=x) + " "); } } }
Sample Output:
Do-While Loop Syntax:
do { Code to repeat multiple times till the condition is met... }while(condition to met);
Do While Loop Example and Character Input Example:
package javasample; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { int num1; //declare variable to hold first input String string = " "; // variable for sting input char ans; Scanner input = new Scanner(System.in); System.out.print("Please Enter Your Name: "); string = input.next();// input for string do { System.out.print("Please Enter a Number: "); num1 = input.nextInt(); // input for integer System.out.println(string + " the even numbers of number " + num1 + " are:"); for(int i = 1; i <= num1; i++) { int x = 1; System.out.print((i+=x) + " "); } System.out.println(); System.out.print("Want to enter again?[Y/N]: "); ans = (char) System.in.read();// to read the input character }while(ans == 'y' || ans == 'Y'); } }
Sample Output:
While Loop Syntax:
While(condition to be met) { Code to be executed if the condition is me }
6. Java Arrays
Arrays are used to hold series of variable and input. Index is what you call to the variables that the array is holding. Index starts at 0. Example: if there are 5 numbers in an array, the index count would only be 4 because the first variable will be the index index 0, the second variable would be index 1 and so on.
To declare an array in Java is like this:
DataType[] arrayname = new DataType[size of the array];
Example:
int[] integers = new int[2];
This array hold two indexes, the index 0 and the index 1.
Other Java Source Code Examples
- Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
- Java Source code sample: How to Add Numbers inside an Array using For Loop
- Java Source code: How to Add numbers inside an Array Using Recursion
- Java source code Sample: Print Different Asterisk Shapes in Recursion
- Java Source Code: A Recursive Asterisk Diamond Shape
- Java Source code sample: Print String Backward using Recursion
- Java Source Code in a Recursive Linear Search
- Java source code: Recursive function for X to the power Y
- Java Source Code: Binary Search in Recursion
- Java Source Code Recursion: Recursive Koch Snow Flakes
- Java Source Code sample: Sort Numbers in Bubble Sort
- Java Source Code Determine Person's Salutation and Current Age