Introduction - Prasan1/Java-Tutorial GitHub Wiki
Introduction/Java Basics
Scratch your knowledge
☺ How many languages do you speak? ☺ How will you communicate with a Spanish who doesn’t speak English? Simple: you speak Spanish.
☺ But what will you do if you don’t know Spanish? Can you still communicate? Yes, you can, but you have to find someone who can convert your English into Spanish and his/her Spanish into English. That somebody is called Translator or interpreter. Ok. Let me ask one more question.
☺ How many computer languages do you know? You may have used computer many times before, but you never had a direct conversation with it. I guessed it right, didn’t I?
☺ What is a Language?
A set of rule by which we express ourselves. Language is the way of expression. A language that you use to propose your girl/boy to marry you is a human language. A language that you use to give a print command to your computer to print a love letter from your computer screen is a computer language.( You can even email that letter to save trees). However, you don’t need to know computer language to print the letter. You will give a command to do so in English (or any other language if available) and a computer will also interact with you in English. Seems very simple right? But in reality, computer doesn’t know English. There is a translator or a interpreter between you and a computer which makes things simpler. The language that we use to interact with a computer is called a programming language. We call it programming because with the help of these languages we make computer do whatever we want it to do.
We use different programming languages to communicate with computing machines and users. You will be clear about programming language after writing a couple of programs. If you want to know then you have to wait.If you want to memorize at first and then learn later. Go and google computer programming articles.
Let's start learning a language to communicate with our machines. We are going to use java programming language for now.
What is Java? Java is an object-oriented programming language.Let's not talk more than this for now. But before moving out of this topic lets list some of the characters of object oriented programming language:
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism
Definition or explanation of these terms will be easy after writing some codes so I am leaving this for later. Start programming in Java:
Before writing a program in Java, we must think about a class.Now, what is a class? A class is a category where your idea of code resides. It is just like listing your monthly expenses. We can categorize them into; Utility, entertainment, rent or so on.You can imagine java class exactly the same. But there are some rules we need to follow while naming the class or category in Java. They are:
- The name must be a noun
- The first letter of the name must be a capital letter.
- Pick a simple name
- Do not use acronym
Java class starts with "class" keyword following the name of the class you have chosen. eg. class First or class Second or class Employee or class ParkingSpot or class PicnicSpot or class YourName etc
The last two name are in a CamelCase in which we capitalize each first letter of the word and this makes your code more readable.
Ok, now what after Class? After the class, we write a method.Now, what is a method? You can think method as a behavior. Suppose you want to write an essay about your pet dog. The title of your essay is like a class and all the qualities of your dog that you are talking about is the method.See the examples below:
class Dog my dog barks he Jumps a lot he sometimes bites my plastic toys
In above examples, I have a dog and I have listed some of the activities of my dog. All these activities are verbs. These verbs are methods. Methods do something.
Now, how do you separate a class from a method ? In java, we use the curly braces to separate class and method. But remember, we put a method inside the class. We end method with () eg. barks(), jumps() , bites() etc. we can put stuff inside () but let's not carry loads for now.
class Name{method} suppose a name of our class is Dog. Now, our code becomes: class Dog{method} Again suppose, one of the methods is bark. Now, our code becomes: class Dog{bark()} or we can even write as
class Dog{
bark(){
You want to put something here
}
}
What goes inside ()? We can put some parameters inside () but hold on to this concept now.
Rewriting above class and method to make the code beautiful:
class Dog{
method bark(){
}//end of method bark
}//end of class dog
if we have to write the same thing in Java, we write as:
class Dog {`
public void bark(){
}
}
Let me explain the above code a little. The first line is obvious. We have declared a class called Dog. We also know that the second line is a method. But what about that public and void thing? Well, in Java there are something called access modifiers. There are mainly three access modifiers in Java namely: public, private and protected. More on these modifiers later. For now, you just need to know, if you use public as your modifier then everybody can access it. Pretty scarry right? void: This is a return type. A method in Java can return an integer, true or false( Boolean value), an array of data, string etc. Here, this method doesn't return anything so it is void. Void means empty.
Take a break:
But before taking this code for further discussion let's use a simple java code to print a line of text( a string of letters or characters ) on the screen.
class HelloWorld {
public static void main(String[]args){
System.out.println("Hey! wats up?");
}
}
Output: Hey! wats up?
In the code above, you will see some weird text which we have not discussed about. These are static, main, String, [] and args.Let's go through each of them one by one. First, in order to run a java program, We must declare a method called the main method. In the code, we have written the main method.This method is almost similar to our previous method bark(). A method name is main instead of bark, both main method and the bark method have an access modifier public and the return type on both is void. But there is something more on this new method. It has a static keyword. Well, static keyword is here to show its association with the class. If you have a method with static keyword in a class then you don't need an object of the class to invoke it. A String is a data type which we will learn later. [] is a sign for a tray to hold multiple data and in Java, we call this tray an array. And args is just a variable name. Try writing your name in place of args. It means, the main method takes an array of arguments of type String. The next line is a statement. This statement is used to print an output to console. Here it prints, Hey! wats up?.
Brushing Up:
How to learn a programming language?
The best way of learning programming is to read code and practice writing them by changing the output or by changing the syntax.Play with a code already written by someone by copying it and pasting it in your choice of IDE and then compiling it. Observe the output and make changes on the syntax. Run it again. Go back to the original code and try writing similar logic with different output.But besides this, if you really want to learn coding then start using a terminal.Ever since I started using a terminal, computer code has become my favorite time pass.A Terminal is the beauty of computer which you can not see until you remove the cover of GUI. Another important point is typing.Your typing speed will play a vital role in learning.If you are quick to take notes then you will be quick to think. If you think quick then you will accomplish a lot in a short period of time. There are lot of websites that teach touch typing.Visit them and practice typing at least an hour a day. If you can type 35/40 words per min then you don't have to worry about this.
In this tutorial, We will be talking about Java. Love it or hate it Java is an indispensable language in your programming portfolio. There will be 2 sections here. The first section will have the basics of Java programming language. Let me be clear in one side of this tutorial though, I am not going to write an explanation of each and every code that I write here. You are responsible for generating semantic and syntactic meaning of all the programs written here. There will, however, be some explanation which you might consider inadequate. Now, Why am I writing this tutorial then? One of the reasons I would say is that I like to share what I have learned in my university. The other reason is I always follow Einstein's quote, "Make things as simple as possible but no simpler."
Points to Ponder: a) Java code starts with a class keyword followed by a class Name. Notice N in Name is capital. Your Class Name must start with a capital letter. b) Inside the class, it has a method called the main method. c) Then we put statements inside the main method. d) Statements always end with semicolon;
so its simple: class Name{ main method{statements;}} or,
*/
class Name {
main method() {
statement;
}
}
- Simple Hello World program.
/***********************************************************************************
Description : This code prints a line of text.
Author : prasanna p
***********************************************************************************/
public class HelloWorld{
public static void main(String [ ] args){
System.out.println("Hello World!");
}
}
Question: /*In the code above, a. What is the class name? b. What is the main method? c. Find a statement in above code. d. Modify the code to print your name instead of Hello World. */
2.Difference between print and println statement
/****************************************************************************************
Description : This code shows the difference between println and print
Author : prasanna p
**************************************************************************************/
public class Difference{
public static void main(String [ ] args){
System.out.print("A line of text.");
System.out.print("This line doesn't get printed in a new line.");
System.out.println("New line starts after this text.");
System.out.println("LOL println is just like hitting ENTER.");
}
}
/* In the code above, What is the output of the code? */
- Comments in java
/**************************************************************************************
Description : Something about comments.
Author : prasanna p
***************************************************************************************/
public class Comments{
public static void main(String [ ] args){
System.out.println("// This is a single line comment. ");
System.out.println("/* ....*/ This is a Multiline comments. ");
System.out.println("Comments are a description of a sourcecode");
}//end of main method
}// end of class Comments
//The Description and the name of the author are inside multi-line comment i.e /* ..*/
/* Question: When do you use multi-line comment? Why is it different compared to inline comment? */
4.Concatenation
/*****************************************************************************************
Description:This code shows string concatenation in java.
Concatenation is to put things together to make a chain.
Author : prasanna p
*****************************************************************************************/
public class Concat{
public static void main(String [ ] args){
System.out.println("You and "+" I are joined with plus" );
System.out.println("Concatnation: Combining two or more text.");
}
}
/QuestionA. WHAT IS THE OUTPUT OF THE FOLLOWING CODE?/
public class Concat{
public static void main(String [ ] args){
System.out.println("4"+"3" );
}
}
//Same old Hello World program in a different way.
/**Description:Simple hello world program *
* @author prasanna p
* */
public class HelloWorld {
public static void main(String[]args){
HelloWorld hello = new HelloWorld();
hello.simpleHello();
}
public void simpleHello(){
System.out.printf("Hello Word!");
}
}
//What is the difference between this hello world program and the first hello world program?
NOTE: This page is not well formatted and still developing ....