Sunday, August 11, 2013

Java Skills Acquired: Declaring Variables/ Multiple Variables, Local Variables, Initializing Variables, and Final Variables (Or Constants)

Today in JFD (Java for dummies), I learned the following

Declaring variables

Example: String example;

Declaring Multiple Variables

Example: String ex1, ex2;

Declaring class variables

(Hints-Must include static in the declaration before the variable type;
must place declaration within the body of the class but not within any of the class methods)

Example of declaring a class variable

class HelloApp
{
    static String hellomessage;
    public static void main(String[] args)
    {
        hellomessage = "hi";
        System.out.println(hellomessage);
    }
}

and this is the output

run:
hi
BUILD SUCCESSFUL (total time: 0 seconds)

---------------------------------------------------------------------------------------------

I'm using the Netbeans IDE 7.3.1 from here on with my tutorials unless otherwise specified by JFD.

I keep having the same problem with the example in the JFD. It specifies each example with "public class HelloApp" It won't build properly unless I take away the "public"

Not sure what public does exactly..

The JFD says that public indicates that a method being declared here should have public access.
Meaning that classes other than the HelloApp class can use it.


Placing the variable at end of class

class HelloApp
{
    static String hellomessage;
    public static void main(String[] args)
    {
        hellomessage = "hi";
        System.out.println(hellomessage);
    }
}

run:
hi
BUILD SUCCESSFUL (total time: 0 seconds)
----------------------------------------------------------------------------------------------------------

Declaring Local Variables
(HINT: is picky about where one places the declaration - Before the first statement that actually uses it)
Example:

class HelloApp
{
    public static void main(String[] args)
    {
        String helloMessage;
        helloMessage = "hey";
        System.out.println(helloMessage);
    }
}

run:
hey
BUILD SUCCESSFUL (total time: 0 seconds)

------------------------------------------------------------------------------------------


The next to last part of JFD in the variable section is Initializing variables.

class testApp
{
    public static void main(String[] args)
    {
        int i;
        i = 0;
        System.out.println("i is " + i);
    }
}

in the example given the variable is initialized to a value of zero and this happens before the println outputs the variable's value.

this was the output of the example in the book.

run:
i is 0
BUILD SUCCESSFUL (total time: 4 seconds)

Last example of a initialized variable... initializing variables with initializers... (initially)

The book states that this is so we can combine a declaration and an assignment statement into one statement.

class testApp
{
    public static void main(String[] args)
    {
        int x = 2;
        int y = 1;
        String lastName = "Lowe";
        double radius = 15.4;
        System.out.println(x + y);
        System.out.println(lastName);
        System.out.println(radius);
    }
}

run:
3
Lowe
15.4
BUILD SUCCESSFUL (total time: 2 seconds)

Now this example has me confused a little.
It was this initially

int x = 2;
String lastName = "Lowe";
double radius = 15.4;

this is a combination of a declaration and an assignment statement...

I tried to compile this to state what each variable is in the println method but, only got an error.

I guess that I can't mix variable types? Because it was able to build after I had did three separate println statements.

(Any clarity on this would be greatly appreciated)

I only think this is correct because when I make a statement of just int variables this happens....

class testApp
{
    public static void main(String[] args)
    {
        int x = 6;
        int y = 8;
        System.out.println(x * y);

    }
}

run:
48
BUILD SUCCESSFUL (total time: 2 seconds)

------------------------------------------------------------------------------------------------------------

phew okay, Final Variables (or constants)

this is easy enough to understand, just declaring a variable that can't change after it has been initialized.

this is done by adding final to the variable declaration.

class FINALVARIABLE
{
    public static void main(String[] args)
    {
        final int WEEKDAYS = 5;
        System.out.println(WEEKDAYS);

    }
}

run:
5
BUILD SUCCESSFUL (total time: 2 seconds)


TO create a final class variable add static before final when declaring a variable.


I doubt there are many still reading this. I still have to mention that this blog is mostly to demonstrate
that I am learning about Java from online resources as well as JFD (Java for dummies) as well as a reference for me in the future.

I'm sorry that this is a long read but, I am still working on a easy to read layout.

Sunday, August 4, 2013

Tutorial One - Testing the Compiler



I bought Java All in one for Dummies to begin learning more about java ISBN: 978-0-470-37172-5

Stuff packed in my brain on 8/4/13

Previously I have installed everything I have used in the past while dabbling in JAVA. I still have 0% experience.
I already have Java Runtime Environment installed on my computer as well as a Java Development Kit and Netbeans
(I used for a few beginner tutorials earlier this year)

Tutorial #1 - Test the Java Compiler

The dummy book began with the Hello World! java example.

Before The tutorial began the book asked me to set the path in system properties > advanced > Environment variable
> PATH > click edit > semicolon after what is already in box > copy and paste the java bin directory then

The Program to test the compiler

The Dummies book was not a step by step in this process and so I had to google what a cd was in a command prompt



This video was great. It directed how to use the CD - Change directory, change class, and how to test run the Java Compiler (Javac in the prompt)

cd\ > dir > cd\test (A folder I saved in C:\ that holds my test java files) > javac HelloApp.java (converts to class file) > java apples > (OUTPUT) Hello Youtube!


Here is the code used on the video

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

and a picture of my output


Java for Dummies or JFD also noted that javaac *.java can compile all the files in a folder 


Feels good to take my first step into Java. Any tips would be appreciated, thanks!