JAVA - Introduction

by JAVA Teacher in Circuits > Software

3292 Views, 27 Favorites, 0 Comments

JAVA - Introduction

java[1].jpg
I will try to guide you people through this tutorial effectively, and quickly. JAVA is a very complex language with a lot to learn. These tutorials do not assume any responsibility for teaching you correctly, but I will try to make it clear, and leave all mistakes out. If you are already familiar with JAVA, you should skip ahead to later sections. This section is just a note. Next we will start getting you welcome with the JAVA language.

JAVA - Getting the Tools

Javva EE.jpg
Java cls.jpg
Java cls.jpg
Now, as you probably know, we need some tools to start writing JAVA code. You could just use your notepad, and execute it from the command line, but we will get an editor, because it is easier and faster. We will get the eclpise JAVA editor. (IF HAVING TROUBLE COMMENT)

STEP #1) Go to the website here and click the green download button

STEP #2) Save it to your desktop and click "OK"

STEP #3) When it finishes downloading, right click the file and select "Extract all"

STEP #4) You should see a new folder appear on your desktop, and make sure it has the file "eclipse.exe" in it.

Now double click on the eclipse.exe file with the icon of a solar eclipse. It will ask you to create a workspace when it opens. Enter "myWork" in the name bar, and click OK. You should then see a welcome screen, and in the top right corner click the "workbench" button.Now you should see something like the 1st image at the bottom. 
 After that click "File" > "New" > "Java Project".
In the name box, type "myProj", and click next, and then finish. Now, in the project explorer(left of screen) you should see a folder called "myProj". The project explorer is where you can see all of your files.The area in the middle is the mainstage(coding section) and the right part is the Library, which gives us a list of functions and classes.(Will talk about classes and functions later). The bottom part is the error list, if we have any run-time or code problems, they will be there. It is also the console window where output is displayed.
 Finaly, right-click the "myProj" folder we created and go to "New" > "Class". In the name bar type "myFirst". Click finish. You should see the 2nd picture at the bottom for a closer look.

 Now you are ready to start writing code in JAVA. In the next step we will write your first program, and discuss some JAVA elements.

JAVA - Getting to Work With JAVA

JAVA method.jpg
Java console.bmp

 In JAVA, everything is based on classes, sections of code with commands to execute. There are also these things called methods,smaller sections of code that contain functions too.Usually there are multiple methods in a class,that interact with each other based on values of certain variables and return a value. Those methods are packed into a class, and then classes with methods can interact with other classes and print the return value on the screen. There is 
also something called a main method, the method the compiler searches for first. Based on the instructions the main method gives, the compiler can move to different classes to execute different methods,or just stay in the main method.
 For now lets just create a main method. In your "myFirst" class type the code in bold:

public class myFirst {
   
     public static void main(String[] args)
     {
  
     }

}


Now lets discuss this code. Each method is based on the following syntax:

[accessSpecifier] [returnType] [methodName] ( [parameters] )
{
  [methodBody]
}

The access specifiers in this case are "public" and "static". Any method can be "public" or "private". "Public" means the method can be accessed by any class. "Private" means that the method can be accessed by only the class it belongs to. I will explain the "Static" key word later.Here we made a public static main method with the name main, and parameters of "String[] args"(I won't explain the parameters now). In the method body we type all of the commands we wan't to execute. The method body's and class body's are always located between the curly braces.
 NOTE: JAVA is a case sensitive language, so when you type commands, you must type them exactly as specified, or you will get an error!!!!!!

 Now type the code in bold into your main method:


public class myFirst {
   
     public static void main(String[] args)
     {
          System.out.println("Hello world!");
     }

}

By now you should have the code in the 1st picture. Now go to "Run" > "Run", and click "OK" when the dialog box appears, and at the bottom(console window) you should see the text "Hello world!" printed. Check the second image for reference.
 Here we used the command System.out.println to print a line on the screen. The "System", is a class containing many functions. The "out" was that we wanted to print OUT to the screen(or output) and the method "println" means; print line. Then in brackets, and in quotation marks(because this is a string value(value containing words)) we included the text we wanted to print,and ended the line with a semi-colon(;). NOTE: All lines in JAVA must end in semi-colons, except lines when we declare classes or methods.
 We can also use "print", but the difference between "print" and "println" is that "print" prints text on a line, but "println" means to print the text, and end the line, meaning that if the next command is "print", the text will be printed on a new line.
 At this point, I would like to apologize for the bad quality of my images.I have included some SELF-CHECK questions at the bottom. In the next step I will include the answers to them.In the next step I will also introduce you to the basic value types.



 SELF-CHECK:

 #1)   Write a program to print the word "cheese" letter by letter.

  HINT: Use the "print" command

#2) Use the "print" and "println" commands to experiment.

#3) What is wrong with this line of code:

     System.out.println(Hello world!);

#4) What will you get if you run these lines of code:

    System.out.print("h");
    System.out.print("i");
    System.out.println("per-");
    System.out.print("son");

 

JAVA - Basic Variable Types

combine data.jpg
console img2.jpg
The answers to the previous SELF-CHECK questions are:

#1)      System.out.print("c");
            System.out.print("h");
            System.out.print("e");
            System.out.print("e");
            System.out.print("s");
            System.out.print("e");

#2) No definite answer.

#3) The text in brackets was not in quotation marks.

#4) hi per-
       son

There will also be self check questions at the end of this step.

 There are many data types. In this instructable we will go over only the basic ones, and it will still take a couple of steps.



All variables work on the syntax below.

[dataType] [variableName] = [value];

ex.

  int myNum = 8;


int type:
 
   
The "int" type, means integer. Works on the same syntax as above. There are no quotes needed to hold the value for any numerical type. Any int variables range from a minimum of -2,147,483,648 to a maximum value of 2,147,483,647. Most common integers will fit in this range, but if they don't use "long" instead.

 ex.

 int nine = 9;

long type:

 
 The "long" type is a long version of the "int" command. Ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

float type:

  The "float" type is a floating-point number, which means it contains a decimal value.

double type:

 
The "double" type is a floating-point number, which can hold a bigger value.

string type:

 
  The "string" type holds a text value. The text(value) must be inclosed in double quotes.
 
   ex.
  
       String greeting = "Hi blank";

 
Those were the basic data types. To print any of them just write the variable name in the parameters of the "println" method without quotes.

 ex. 
  
    int myNum = 52930;
    System.out.println(myNum + "Is the value of myNum");

The code above would print "52930 Is the value of myNum" on the screen. And by the way we used there a plus sign to combine a String to the line we were printing, so it would print a String value after the value of myNum. You can use the plus sign to add variables in the "println" command and add string values. Check out the two pictures at the bottom to see what I did.

This is section 1/2 of the number types, in the next section I will teach you some simple mathematical operators you can use on the variables.

JAVA - Mathematical Operators

int string.bmp
int string result.jpg
product code.jpg
product result.jpg
This is section 2/2 of "Basic variable types". Here I will introduce mathematical operators. There is the "+" sign which means addition. It is used for adding numbers.

 ex.

 int sum = 5 + 579;

It is also used to combine strings in the "println" method.

 ex.

 System.out.println("This is " + "three strings " + "combined.");

Notice that before adding another string on the first and second strings I used a space at the end to make it look normal.

There is also the "-" sign as you have guessed, and it is used only to subtract numbers.

 ex.

 int subtraction = 9 - 6;

 Also there is the multiplication operator, which is represented by a "*" in java(asterisk). It is used to multiply numbers.

 ex.

  int multiplication = 756 * 15;

And there is the division operator, which is represented by the "/"(slash). It is used to divide numbers.

 ex.

 int division = 50 / 5



Also there is a modulo operator, which is represented by the "%". Modulo is used to focus on the remainder of two numbers, if there is any.

 ex.

 int modulo = 10 % 9;


You do not need to add quotes for the numbers if you use the numbers in the "println" method, or they will be interpreted as string values.

ex.

 System.out.println(6 + 7);

 COMMON ERROR 1:

 System.out.println("6" + "7" );

  The code above returns 67, not 13. To avoid this delete the quotes.


 The variable names can be used to identify values. Such as:

 int myNum = 9;
System.out.println("The value of myNum  is " + myNum);

 As long as "myNum" doesn't have  any variables around it, the program will print "The value of myNum is 9".  You can also use the operators to perform operations in the "println" method to return quick results.

 ex.
 
 System.out.println(8 * 10);

My pictures will be basicly on everything we covered in this section, but don't forget to check them out. In the next step there will be little new material, but there will be a test that covers everything we learned so far.  Here are the self check questions:

 SELF-CHECK #1:

  Write a program to calculate the modulo of 789 to 2, and print the result on the screen.

 SELF-CHECK #2:

 Describe the "int" data type, with at least the basic characteristic.

 SELF-CHECK #3:

 Create a string variable called "greeting" with a friendly message in it leaving out the name(ex. Hello _______). Then create a string called "name" with the value of your name. Then combine these variables and you should get your final message.

 SELF-CHECK #4:

 How do you represent multiplication in JAVA?(What sign do you use)



 

 





 
 



JAVA - 1st Test / Commenting

quiz.jpg
Here are the answer to the previous SELF-CHECK questions:

#1) System.out.println(789 % 2);

#2) The "int" data type holds an integer.

#3) String greeting = "Hello ";
       String name = "JAVA Teacher"
       System.out.println(greeting + name);


#4) You use an "*"(asterisk)

 OK, now for this instructable I will only include a little new material, and the link to my test.

 In JAVA there is something called "commenting". That means to comment your work.
 There are 2 types of comments you can make a single-line comment(see ex. 1) and a multi-line comment(see ex. 2) . The examples for these comments are included. For a single-line comment you have to put 2 slashes before the text, everything to the right of the slashes is considered a comment, and ignored by the JAVA compiler. A simple multi-line comment is in between the slash and 2 asterisks, and ends with the asterisk and a slash. An advanced multi-line comment discribes a method, we will go over this later.

 JAVA ADVICE:

 
  I suggest you to comment everything, even the simplest things. Because if someone is going through your work and may have trouble understanding your code. It might not be obvious that the variable d stands for dollars . And I also suggest you to save your work frequently.(I lost a lot of code because of this once)


 ex. 1
 
 int num2 = 78; //Create an integer, "num2" with the value of 78

 ex. 2

 /**
     Create an integer, "num2" with the
     value of 78

    */
  int num2 = 78;

OK, good luck on the test. :-)  (LINK AT BOTTOM, READ NOTE)

NOTE:

 I really rushed through making the quiz, so on #2 I marked the wrong answer as right. The correct answer for that one was the last option. I am very sorry for this inconvenience.



The link to the test is here. There's a picture at the bottom of the welcome screen of the test too.Good luck and don't forget to read my next tutorial! :-)