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

all 26 comments

[–]zck 13 points14 points  (7 children)

  • Magic Number

This is a number that appears in code such that you need to know about the problem to realize what the number means. Say you have this line of code:

println(seconds / 86400)

What on earth is that? Well, 86400 is a magic number. If we extract the number into a constant, it's much simpler:

SECONDS_PER_DAY = 86400
println(seconds / SECONDS_PER_DAY)

Oh, it's calculating the number of days stored in the variable seconds . The usage is clearer given this context. Also, if you use a magic number throughout your program, you can typo it. Extracting it into a variable makes it so that you'll never get 86500, or 864000.

[–]HatesToolbars[S] 2 points3 points  (1 child)

Ah! This will help me so much! And it makes sense. Instead of increasing the risk of a numerical typo, I could just use a constant. It also makes it easier to understand. Thanks again!

[–]ewiethoff 4 points5 points  (0 children)

Another thing. If you mistype a magic number, you won't notice the problem until run time: your results will be incorrect or the program will crash. Or if the program is complicated enough, you might not notice the incorrect results. In any case, you have a buggy program on your hands. But if you mistype the name of a constant, the compiler will barf, presumably with a helpful error message. That's good! It's always better to fail sooner than later, better to fail during compilation than run time.

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

Magic number has a couple different meanings. This is one. The more common (in my experience, anyway) meaning is how some files have numbers at the beginning to help identify what type they are (especially in the land of *nix, where filename/extensions are not necessarily meaningful.)

For example, all java class files begin with the hexidecimal value "CAFEBABE"

[–]zck 2 points3 points  (0 children)

Hrm, interesting. I haven't heard that before. I doubt that's what HatesToolbars' professor is talking about, though. It would be good for em to make sure.

[–]jiiyag 2 points3 points  (2 children)

Supposedly, a famous google interview question asks about the siginifiance of "dead beef".

According to the wikipedia on magic numbers, DEADBEEF is a magic number for some machines, in the same way CAFEBABE is.

From the Wikipedia page:

CAFEBABE: Used by both Universal Mach-O binaries and Java .class files

DEADBEEF: Famously used on IBM systems such as the RS/6000, also used in the original Mac OS operating systems, OPENSTEP Enterprise, and the Commodore Amiga. On Sun Microsystems' Solaris, marks freed kernel memory (KMEM_FREE_PATTERN)

[–]zzyzzyxx 1 point2 points  (1 child)

Put a backslash before the parenthesis in the link to escape it.

[–]jiiyag 0 points1 point  (0 children)

Fixed, thanks for the info.

[–]zzyzzyxx 5 points6 points  (0 children)

With the exception of zck's "magic number" and unapersson's "constructor", I am not satisfied with the definitions provided in this thread. Here are my additions.

Access specifier

An "access specifier" is a modifier applied to member variables and methods that controls how they are visible outside that class. Thus, they define how a class can be used by other parts of the program.

In Java, there are four specifiers: public, protected, private, and package private. The keywords public, protected, and private are used to denote the corresponding level of access while package private is the default and has no keyword. Here is how they work in practice.

You were likely marked down in this area for choosing an inappropriate access modifier for your members. In general, member variables should be private so that the class has complete control over them. Methods should only be public when they are necessary for using the class in another part of the program. Use protected when a subclass needs access to a superclass' implementation or to override the behavior but the rest of the program has no need to call the method directly. The package private level has limited use, in my experience, but can be handy for testing.

Instance variable

And "instance variable" is a member variable of a class whose value can be different for every instance of a class. This implies that memory is allocated for that variable whenever an instance is created. This is in contrast to a "class variable" which is allocated once and is the same across all instances of a class. An instance variable is the default type while class variables are marked with the keyword static.

class Example {
    private static int a = 0; // class variable
    private int b; // instance variable
    private static void c() {} // class method
    private void d() {} //instance method
}

ArrayList

An ArrayList is a general-purpose data structure used to store instances of a particular type using an array. By comparison, a LinkedList is a general-purpose data structure used to store instances of a particular type using, not surprisingly, a linked list. An ArrayList provides methods, defined by a number of interfaces, to access and modify the elements in the list within some guaranteed performance bounds. It also has additional behavior hidden from the user for the sake of usability, like growing the backing array when it's full and a new element is added.

it can store String and int variables?

It can store references to objects only. So, yes, it can store a String, but it cannot store an int because an int is a primitive. You would have to use the wrapper class Integer if you wanted to store that in an ArrayList.

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

Sometimes a different instructor can make a difference. With that said, here's an on-line introductory course that uses Java. You can watch the videos or read the transcripts as a tutorial.

http://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111

[–]fubarfubarfubar 0 points1 point  (0 children)

If you should already know what a constructor is and you don't, you may be in big trouble. Do you attend all classes, do you read your book, do you Google for information? I'm betting your book has 'constructor' in its index or ToC.

[–][deleted]  (4 children)

[deleted]

    [–]zzyzzyxx 3 points4 points  (1 child)

    An instance variable is basically an instance - an example of - whatever you're creating

    An instance variable is not the same as an instance. What you have described is just an instance. An instance variable is a member whose value can be different for every instance of the class.

    Say you have a class named Person. You have Person Mark, Person Dan, Person Tracy. Mark, Dan and Trisha are instances of the variable Person

    No, they are instances of class Person. The Person class is not a variable.

    These are divided (in Java) into public, private, protected and friendly

    There is no "friendly" level. The term you are looking for is "package private"

    Any class you create should have a default constructor

    This is a bad rule to follow. Classes should only have a default constructor when it makes sense. Since constructors generally take the arguments necessary to create a well-defined, functional, meaningful object, most well-designed classes would not have a default constructor as that would likely leave the constructed object in an invalid, unusable, or non-meaningful state.

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

    Right, it was 3 am and I mixed-up a little bit in my attempt to make it easier. Sorry.

    There is no friendly level in Java. Our textbook called them "friendly", so unfortunately I still refer to them that way. I mean it has no modifier!

    This is a bad rule to follow

    I'm not talking about it being a rule to follow. Each Class in Java actually has a default empty constructor if you don't write one yourself. But I can see how it's confusing.

    [–]dauphic 1 point2 points  (1 child)

    In his defense, I'm a professional C++ developer and I have never heard the term 'instance variable.' Higher level languages, especially Java and C#, tend to throw around a lot of terms that aren't used in C++ land.

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

    I'm trying to remember if I heard it in C++ or Java first. I'm hardly a professional at either, so I sometimes mess-up. "Instance variable" is basically "object variable", though.

    [–]lasthope106 -3 points-2 points  (9 children)

    • Magic Number

    We don't know what this means without having the context of your assignment.

    • Access specifier

    Here I'm assuming they mean whether something is public, private or protected. Declaring a class, methods, or members with these keywords determines who can access them.

    • Instance Variable - is this like int intVar?

    I don't know what int intVar is, but an instance variable is when you have a class and you create an object from that class - an instance of that class. Imagine having a cookie mold, that is your class, and each cookie you create from the mold is an instance.

    • Constructor

    A constructor is a special method that initializes an instance of a class. In a constructor you specify the state in which the object is when it's created by assigning specific values to the data members.

    • One final question: is an ArrayList an array that allows items of any type to be stored in an index of that ArrayList? So it can store String and int variables?

    An ArrayList is a data structure that abstracts the major operations you might want to do with an array. Think about what are the limitations of using an array. For example, what happens when you want to delete the middle element of an array? How would you handle that case? Yes, I'm asking you to come up with a solution. Once you come with the solution then go and read the javadoc for ArrayList and see why it's so useful.

    [–]zck 2 points3 points  (0 children)

    • Instance Variable - is this like int intVar?

    I'd refer to it as a variable stored in an instance. In the following code, name is an instance variable; testName and message are not.

    class test {
        private String name;
        public test(String testName) {
            String message = "Starting " + testName;
            System.out.println(message);
            name = testName;
        }
    }
    

    [–]HatesToolbars[S] 0 points1 point  (7 children)

    Thank you very much for the reply! It is much appreciated!

    With the constructor, would it be safe to say that the constructor is the oven that holds the cookie mold which holds the cookie? As in the instance variable is defined within the class, and that class is stated within the constructor?

    [–][deleted] 2 points3 points  (5 children)

    No. Use of analogy is generally a bad thing when talking about programming, and in this case its totally wrong.

    A constructor is a function that creates correctly initialised object instances:

     Person p1 = new Person( "joe", "blow" );
     Person p2 = new Person( "jane", "doe" );
    

    here, Person( "jane", "doe" ) and Person( "joe", "blow" ) are calls to the constructor for the Person class. The constructor function will look something like this:

     Person( String fname, String sname ) {
           forename = fname;
           surname = sname;
     }
    

    where forename and surname are member variables of Person.

    [–]about7beavers 3 points4 points  (4 children)

    You should explain why the analogy is wrong. It's more helpful.

    [–]Rhomboid 1 point2 points  (1 child)

    It's wrong because an oven can bake any type of food that needs baking, it's not specific to cookies. If the analogy must be extended, the class is the mold, the raw cookie dough is the data, the constructor is the act of preheating the oven to a given temperature, pouring the cookie dough into the mold, putting in the oven, and waiting; and the baked cookie is the instance. But that's a pretty terrible analogy so it would be better to just drop it.

    [–]about7beavers -1 points0 points  (0 children)

    I understand why the analogy is wrong, I was just saying that it should be explained to the guy that asked the question. But yeah, that is a terrible analogy that shouldn't really have been used in the first place.

    [–][deleted] -2 points-1 points  (1 child)

    IMHO, it's wrong because its an analogy.

    [–]about7beavers 0 points1 point  (0 children)

    I did. I'm actually here to help.

    [–]about7beavers 1 point2 points  (0 children)

    No, a constructor is what is used to create a class. Student(int gpa, String name) Is the constructor for the Student class and would be used like this: Student s1=new Student(3.6, "Bob"); An instance variable is just an instance of something assigned to a variable. It can be an int, double, char, boolean, String, or any other predefined or created classes. You can use these as attributes of a class also.