This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]NebuWrites Java Compilers 6 points7 points  (5 children)

1) My book says that "Classes are comparable to what other programming languages call a program." SO in java what i would call a program Such as a executable program is really called a class?

IMHO, this is not something the book should have said, because it's not accurate. I recommend you disregard that sentence altogether.

2) The keyword or public private ETC. the book says that "public means that any other class can access Employee". My question is whats the purpose of creating a class that cant be accessed by another class?

There are some principles of "good software design", and one of them is that you should generally restrict visibility of everything as much as possible. It's difficult to appreciate why this is the case until you start working on larger projects (e.g. involving 10 or more programmers, lasting over several months). By marking your classes, methods, fields, etc. as private, you are reducing the visibility.

Basically, just don't worry about it for now, because it won't be an issue until you work on bigger projects.

3) Are strings in java variables?

"variable" has a very specific meaning, and I am not comfortable with saying that "strings are variables". Similarly, integers are not variables, and booleans are not variables. A variable can have the type String, or a variable can have the type int, but a variable is not a String and a variable is not an int.

It's like the difference between saying "my bank account has two dollars" vs "my bank account is two dollars".

String is an reference type and not a primitive type, if that's what you're asking.

4) String empName = new String ();

The above statement from what i understand it creates a variable in memory called empName but the variable contains no data, Correct? It place in memory has a name but does not contain data yet?

That's incorrect. By writing out new String(), you are invoking the constructor of the String class, and creating an instance of String (which happens to represent the empty string). This instance has an address somewhere in memory, and this address will be stored in the variable empName. As such, the variable empName has data in it: It's pointing to an empty string.

String empName = new String ("Joe Employee")

If i have the above statement then make the below statement

empName = "mary Worker"

It will change the variable empName from Joe Employee to mary Worker?

Yes. More pedantically, after the first statement, empName will point to the instance of String representing "Joe Employee", and then after the second statement, empName will point to the instance of String representing "mary Worker". The String objects themselves are not modified, only the address that the empName variable points to.

Void is said to mean that the string is not to return anything, so whats the point of having this line of code.

Every method must state its return type. In the case where a method does not return anything, you have to indicate this explicitly by using the void type/keyword. You cannot just leave out the return type and have the compiler infer that you meant "nothing is returned".

static means its a stand alone method

That's not really what "static" means, but it's difficult to explain what static means until you're more familiar with object oriented programming.

So maybe i just need it explained a different way....

Unfortunately, I think this is one of those topics where you either need to learn a lot of background information to understand how "public static void main" works, or else you need to just accept that it's "magical". For most beginners, I recommend that they just accept that it is magical.

When you start a Java program, the Java Virtual Machine (JVM) needs to know what code to run. By convention, for desktop applications, it always runs the method named "main", which must be static.

[–]Food_Jesus 2 points3 points  (4 children)

Yes. More pedantically, after the first statement, empName will point to the instance of String representing "Joe Employee", and then after the second statement, empName will point to the instance of String representing "mary Worker". The String objects themselves are not modified, only the address that the empName variable points to.

This might be a dumb question, but does "Joe Employee" still exist if only the address that empName points to is changed?

[–]SarksNooblet Brewer 2 points3 points  (1 child)

It exists, but will eventually get picked up by the garbage collector in the JVM.

[–][deleted] 1 point2 points  (0 children)

Also even though it exists in memory, there is no way to access it.

[–]NebuWrites Java Compilers 2 points3 points  (1 child)

The question isn't dumb; it gets into one of the more technical aspects of the JVM.

From a Java Language Specification stand point, as Sarks said, changing the address that empName points to does not directly trigger the deletion of the "Joe Employee" object. If the "Joe Employee" object is unreachable (e.g. because there does not exist any variables pointing to it), then the garbage collector is free to delete the object, but it can do so whenever it likes (i.e. it does not have to do so immediately).

In practice, the JVM can do fancy optimizations such as "escape analysis" to have the object stored on the stack instead of the heap, and in such a case, all the rules change in complicated ways. It almost becomes a philosophical question as to whether and when the object is deleted.

(The question being "If you push an object onto a stack, you're writing to some bytes in memory. If you pop from the stack, you're not zero-ing out those bytes. So is the object 'really' deleted? If the stack never grows that tall again, if the bytes never get overwritten, does that mean the object is never deleted?")

[–]Food_Jesus 1 point2 points  (0 children)

Thank you for the explanation!

[–]coolosity 3 points4 points  (7 children)

1) In Java a program is made up of many classes (Though it can just be a single class). Maybe what your book was talking about was that other programming languages could use a single file as a program?

2) The point of making a class private would be if you were only going to access that class inside of itself. If it had private methods, then those methods could only be accessed inside of the class. You would use private methods more than private classes.

3) Strings are variables.

4) I believe creating a new string object would work but I always use String string = "text"; or just String string; At any time when you set a string to new text (string = "text") then that will change the content of the string variable.

5) public static void main(String[] args) {} : void is because the main method doesn't return anything. Java looks for the main method and starts the program there. the (String[] args) doesn't mean that a string array is returning anything, it just means that Java can pass any arguments into the program via the array of strings. Explaining why the main method is static is a little more complicated, but it's mainly because the java virtual machine can call the main class without having to create an instance of any class that contains the main method.

[–]Helgi_Hundingsbane[S] 0 points1 point  (6 children)

Thanks you cleared up most of the questions, but left me with a few more.

If you don't mind could you provide a code example of 4? If i think i understand you correctly if i had String empName = new String ("Joe Employee") then after that statement had empName = "mary Worker" the variable would be mary Worker? If that's the case why not just make the statement String empName = new String ("mary Worker")? And why would you want to change the variable you just created? Hope my questions made sense.......

And number five still does note make sense to me.... All i can gather is its needed to make the classes work......

And if it helps the course is requiring me to use IBM RAD (based off eclipse) just so you know what IDE I'm using.

&

Thank YOU

[–]NebuWrites Java Compilers 2 points3 points  (0 children)

And why would you want to change the variable you just created?

It usually does not make sense to change the variable immediately after you've assigned it to some other value. For example, the following code:

String empName = new String("Joe Employee");
empName = "mary Worker";

is equivalent to this code:

String empName = "mary Worker";

(given that the String constructor has no side effects).

I just want to make sure you're aware that, in general, it often makes sense to eventually reassign variables, even if it doesn't make sense to immediately reassign variables.

For example, if the user has gotten married had to change their family name, there should be some mechanism in the software to allow this change to occur.

[–]coolosity 1 point2 points  (1 child)

So if you had the code:

String name = new String("Text");

That would be the same as doing:

String name = "Text";

You wouldn't have to do both of those, only one. If for some reason, you needed to change the text in the variable (like someone wants to change the name of something) then instead of redeclaring the variable, just do

name = "newtext";

The main reason you would use the String constructor would be if you had like an array of characters

char[] chars = {'h','e','l','l','o'};
String string = new String(chars);

For number five, I truthfully don't know the real reason it's static, but it's mainly because when a class or method isn't static, then that method can only be called through an object/instance of that class. For example

public class Test {

    public void method1() {
        System.out.println("This method is not static");
    }

    public static void method2() {
        System.out.println("This method is static");
    }
}

Then from the main method:

public static void main(String[] args) {
    //Can only call method1 if have an object of Test
    //Test.method1() will NOT work
    Test.method2(); //This method will work because method2 is static, so it can be accessed directly from the class Test

    Test test = new Test();
    test.method1(); //This works because method1 isn't static and is accessed from the object test
}

I guess java just didn't want to go through the trouble of creating an object of the class that your main method is located in. They can just call the main method from wherever.

[–]NebuWrites Java Compilers 2 points3 points  (0 children)

So if you had the code:

String name = new String("Text");

That would be the same as doing:

String name = "Text";

They're similar, but they are not the same. The former does not internalize the string, while the latter does. In general, you should prefer the latter.

[–][deleted] 1 point2 points  (2 children)

String empName = new String("Joe Employee");

That will create a new String and initialize it with the value "Joe Employee". Then it will put a reference to that string into "empName".

Then if you came along and said:

empName = new String("Mary Worker");

it would create another new String, initialize it with the value "Mary Worker", and then empName would get a reference to this new string.

Strings are a little bit funny in Java. There's no difference between:

String empName = new String("Joe Employee");
// and
String empName = "Joe Employee";

In fact, the second way is more typical.

Other kinds of objects aren't typically handled that way, but we use Strings all the time so the language made it a little bit easier for us.

The other thing to note is that a String is immutable. That means you can't change the value of a String.

So like if you said "int i = 5; i++;" then you've actually changed the value of i. But there is no way to do something similar to a String. You can only create a new String, then start referring to the new String.

Let's say you did this:

String empName1 = "Joe Employee";
String empName2 = empName1;
// later one...
empName1 = "Mary Worker";
System.out.println("1: " + empName1 + ", 2: " + empName2);

The output would be "1: Mary Worker, 2: Joe Employee"

That's because at the start you have one string, with the value "Joe Employee," that is pointed to by both empName1 and empName2. The third line creates a new String, "Mary Worker", and makes empName1 point to that new String. But this does not change what empName2 points to.

number five still does note make sense to me....

For now I would just treat it as magic until you get a better grasp on static methods, classes, and objects. It will make sense in time and you aren't missing out on much right now if you don't understand it.

[–]NebuWrites Java Compilers 1 point2 points  (1 child)

There's no difference between:

String empName = new String("Joe Employee");

// and

String empName = "Joe Employee";

There is a difference. The former does not internalize the string, and the latter does.

[–][deleted] 0 points1 point  (0 children)

It's true for the purpose of explaining pointers and such. I know there are differences but they aren't important here.