Life long sharing . . .

Archive for the ‘Java’ Category

Here is a summary from http://www.tutorialspoint.com/java/switch_statement_in_java.htm

Lesson6-note1

Now, look at the below switch codes. What do you the output will be?

Lesson6-code1

No. It is NOT “You passed. Your grade is D”. The output is:

Lesson6-output1

Why? Because there is a missing “break;” after line 16 in the code. The statements following that ‘D’ case will execute until a break statement is reached, which is at line 19.

Advertisements

This website has a comprehensive list of Java operations: http://www.tutorialspoint.com/java/java_basic_operators.htm.

Here are some of the frequently used one.

Lesson5-note1

Lesson5-note2

Let’s do some quick exercise.

Lesson5-code1

What do you think is the output of the above codes?

Lesson5-note3

Let’s try this code. Can you first guess the output?

Lesson5-code2

Lesson5-note4

Lesson5-note6

Try to understand this code:
Lesson5-code3

Now, can you add a String variable called color, and assign the value of “red” if (a is less than 5 and b is more than 20), and “blue” if (a is greater or equal to 10 or b is equal to 30). Add this line of code in line 8 and line 11. You then add a System.out.println code to display the value of color.

What is the output?

Lesson5-note7

This may seem a little hard for you now, but we will revisit this again.

Lesson5-code4

Another thing to learn about Java programming language is “Modifier Types”. This website: http://www.tutorialspoint.com/java/java_modifier_types.htm has a very good coverage of it.

Lesson4-note1

Let’s try some quiz on the Java Control Access Modifier.

Quiz 1:

Lesson4-code1

Note: use below command to compile and classes in package

compile

For the codes in the main method in AnotherClass class, which lines are valid and which lines are not?

Valid:

Not valid:

Quiz 2:

Lesson4-code2

What is the output of the above codes?

Output:

Quiz 3:

Lesson4-code3

Try to run the above codes. What did you get? How to fix it?

.

Lesson4-note2

Here are details of final, abstract and synchronized modifiers. It might look a bit hard to comprehend now, but we will come to them again in future.

Final:

Lesson4-note3

Abstract:

Lesson4-note4

Synchronized:

Lesson4-note5

 

This site has a good lesson for Java – Variable Types: http://www.tutorialspoint.com/java/java_variable_types.htm.

variables

datatypes

referencedatatypes

variabledeclaration

Try compiling the below code, what is the error ? Try to fix it to make it work.

lesson3-code1

Now, as a revision for Variable Types,

lesson3-note1

try to spot the different type of variable in the below Java code.

Lesson2-code1

  1. Local variables:
  2. Instance variables:
  3. Class variables:
  4. Which line do you think will throw error when the above code is compiled ?

 

This site has a good lesson for Java – Objects and Classes: http://www.tutorialspoint.com/java/java_object_classes.htm.

objectclass

Another important exam note to remember is: the 3 ways to create an object from a class:Lesson2-note2

Quiz: From the code below, which object is created by Declaration? Instantiation? Initialization ?

Lesson2-code2

Before getting our hands dirty doing the coding, first, let’s go through this brief intro.

Lesson1-note1

Now, let’s go through the basic syntax for Java programming:

Lesson1-note2

You may ask, “What is Java identifier?”

Lesson1-note3

And then, you might ask, “What is Java keyword?” And here they are:

Lesson1-note4

Now, let’s do our first coding.

Lesson1-code1

  • Open notepad and add the code as above.
  • Save the file as: MyFirstJavaProgram.java.
  • Open a command prompt window and go to the directory where you saved the class. Assume it’s C:\.
  • Type ‘ javac MyFirstJavaProgram.java’ and press enter to compile your code. Beware! There are errors in the codes! Correct them. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
  • Now, type ‘ java MyFirstJavaProgram’ to run your program.
  • You will be able to see the below printed on the window.

Lesson1-code1_output

Reference: 

https://www.udemy.com/java-tutorial/

http://www.tutorialspoint.com/java/


Advertisements

Archives

February 2018
M T W T F S S
« May    
 1234
567891011
12131415161718
19202122232425
262728  

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 185 other followers