FAQ CS1 Week 2

Muddiest Points from Lecture Material

  1. Does // mean to execute a command?

    No, just the opposite! // means do NOT execute this line as a command!

  2. What's the difference between the file name and the class name in Java?

    In the absence of any package statements, Java expects a class with the name "Xxxx" to be created in a file named "Xxxx.java". When the java compiler "javac" is invoked by saying

    javac Xxxx.java
    or by having emacs invoke it, a file containing java bytecode is the output of that operation and it's name is "Xxxx.class". This is the file that gets executed when we type
    java Xxxx
    or have emacs run our program.

  3. I understand all the information in class, but I have no clue how to take it and use it on the computer. Where to start? How to start? And, what to do?

    Have a little patience. You will create and execute your first Java program in lab this week! If you want to try something before that, go back to the course page and download the TryIt program. Try then to modify it so that it becomes HelloWorld.java. Then try to compile it, and execute it.

    The next step would be to alter what gets printed to the screen.

    The next step would be to declare and initialize some primitive type variables and to print out their values.

    Just keep practicing and building!

  4. I understand order of operations, but I don't understand math.

    So, what are you going to do to change this?

  5. I don't understand 814 % 6 (double), that stuff?

    Well that certainly is clear! :-) Is there a question here? % is the mod operator; it returns the remainder after division. (double) is a casting operator. It always appears BEFORE a number or variable, not at the end. It tells java that you wish for it to use whatever follows the cast as a double, regardless of its current (primitive) type. It sounds like you need to do some reading AND get some one-on-one help. Don't wait!

  6. I don't understand the difference between the <expression> as in
    <variable> = <expression>;
    that you used on your slide and the use of parentheses around expressions.

    This is a reasonable statement! The <expression> terminology is a formalism (BNF or Backus Normal Form) you'll see more of as you get into the theory of computer science. It is a way of saying anything that meets the definition of an expression can go here. In general, an expression can be a literal, a variable, or a combination of both combined using appropriate operators and altering precedence with parentheses! Likewise anything that meets the definition of a variable can go in place of <variable>. In other words, the term between the < and > is a placeholder while everything else is part of the syntax, such as the =.

  7. What is the syntax for writing all of the mathematical statements?

    Surely this isn't really what you expect me to answer as there are an infinite number of possibilities! In general, you translate a mathematical statement directly into Java by adding an "*" whereever multiplication is indicated, using a "/" whereever division is indicated, paying attention to precedence rules, and paying attention to make sure that the types of any operands (literals or variables) give you the type of results you need. For example, if division is being done and both operands are int, you need to be aware that you will be throwing away any fractional part. If you need that fractional part, you will need to cast one of the operands to float or double. Hopefully this week's lab exercises were helpful in getting this point across.

  8. Where do identifiers get used in an assignment statement?

    Identifiers may appear in many places in an assignment statement. You certainly MUST have one on the left side of the assignment operator and it must be a variable. On the right side, in the expression, you might find identifiers of many types, such as constants, variables, and method names.

  9. How do you tell a byte, double, float, etc. apart?

    Well, if you are READING a java program, you can tell what type a variable is by looking at its declaration. But, I'm not sure that's what you are getting at. If you are really asking, "When do you use what, then it really depends on what kind of value you are trying to store (integral or real) and what its range might be (lowest and biggest value). If you know that, you can look at the chart in your notes and choose the most representative type.

  10. I did not understand the mantissa and exponent of a real value.

    These are terms you should have already heard/seen in high school math. But, if I look at the number 1.5e5 in java, this means 1.5 times 10 to the fifth power. The 1.5 is what gets stored as the mantissa and the 5 (the fifth power) is the exponent.

    Look at http://www.psc.edu/general/software/packages/ieee/ieee.html or at http://en.wikipedia.org/wiki/IEEE_floating-point_standard for more information.

  11. I don't understand hexidecimal code. Can you help?

    Look here for detailed information: http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html

  12. I don't understand what literals are and their usage.

    Literal constants appear as a value in an (arithmetic) expression in place of a variable, e.g., x = 5; or y = x + 10;. The Java compiler must allocate a place in memory for these literals. The 5 and the 10 here are both int literal constants. In general, in Java, if the literal has a decimal point, it gets stored as a double (See diagram below.) If not, it gets stored as an int. We can force storage of integral literals as longs by adding an L or an l at the end of the literal, e.g., x = 5L;, or a real valued literal as a float by adding an F or an f, e.g., y = 3.5f;.

  13. The whole idea of saying: x = 2.5f is a little confusing.

    Whether or not you have to say x = 2.5f depends on which type x is. You really ONLY need to say it if x has been declared as float. That's because in java the default type of the literal 2.5 is double, not float, and a float doesn't have enough ROOM to store a double in it.

  14. You showed an example about constants. Why did you use a literal of 2.3 for PI_FLOAT? Shouldn't you use 3.1?
    final float PI_FLOAT = (float) 2.3;

    Yes. The REASONABLE literal to use is 3.1. However, this is LEGAL Java, but it does serve to point out the importance of using MEANINGFUL identifiers in any program you write. So, in this case if we really wanted this to be a 32 bit representation of PI, then we might have caught our error in this way. But, if we were just not naming things in a meaningful way, as is the case here, any future readers of our code will be confused like you were! :-)

  15. You showed an example about constants. Why for a double value did you use
    final double PI = 3.1415;
    and for a float
    final float PI_FLOAT = (float) 3.1415;

    I presume that what you are really asking is why we need to cast the literal in the second case, but not the first. Remember that in Java, all literals containing a decimal point are of type double. If we tried to assign 3.1415 to a variable of type float, there isn't room to contain it all. Therefore, if we tried to make the assignment directly, we would get a compilation error. The casting tells the compiler that we know what we are doing and yes, it is acceptable to loose some precision by making this assignment. Personally, I would probably write this statement either as

    final float PI_FLOAT = 3.1415F;
    i.e., use a literal constant of the appropriate type, or as
    final float PI_FLOAT = (float) PI;
    i.e., use what has already been defined. (In my experience, it is more usual to see casting used with variables than with literals.) In all three cases the result is the same!

  16. Why doesn't Math.PI need to be Math.PI()?

    Math.PI() would tell the compiler to look for a method named PI in the Math class. Because it lacks the (), the compiler interprets PI to be a property of the Math class, either a class variable or a class constant.

  17. Why can't Java automatically arithmetically promote a float to store it in a long? The long is a bigger storage area than the float; there should be room.

    There's certainly is a larger number of bits, but there are two problems:

  18. The sections on precedence and operators are somewhat vague to me as someone approaching this for the first time.

    Just think of it this way, when you do mathematics there are rules for the order in which operations happen, and if there is more than one of the same kind of operator in an expression, there are rules for the order in which they get applied. This is true as well for Java and the rules in Java match many of the rules in mathematics.

  19. I don't understand why there are all these primitive types, the difference between them and how to use them.

    That's a pretty big order for this page! Have you tried reading your textbook? Anyway, the big difference is between the amount of space that is provided for each. (See the figure below.)

    We have ALL these primitive types so that we can effectively model real world entities. You decide WHICH to use dependent on the range and type (integer or real) of the numbers that the real world entity you are modeling might take on. For right now, you'll be pretty safe sticking to ints and doubles, the defaults for numberic literal constants in Java. But to be truly effective and efficient in the future, you'll need to know/use more of the primitive types.

  20. Why would you ever use anything other than a double since it has the widest range?

    In programming, we always try to model the data as accurately as possible to match its corresponding real life entity. The double type does have some drawbacks of it's own, in particular, for modelling real world integer values. Because of how the computer stores double values (see me if you want to know more!), it may not be possible to exactly represent an integer value as a double. And, there's the possibility/probability that the resulting round-off errors will grow when performing arithmetic operations on them. This means that had we done the arithmetic in integer mode, the answer might clearly be a 1.0 (for example), but doing it using real arithmetic we might get 0.99999999.....53. Getting this back to a whole number becomes cumbersome!

  21. The concepts of arithmetic promotion, assignment conversion and casting are confusing.

    Perhaps it would help think about these concepts in two pieces:

  22. My real muddy point in lecture was pre/post increment/decrement operators. I was totally lost at that point.

    The expression


    is the same as saying

    a = a + 1;

    as is the expression


    The difference between them is WHEN the 1 gets added to a's memory location. It doesn't matter if we simply use the expressions above, but if we use a++ or ++a as a term in an expression, then it does. For a++, the value of a used for the overall expression is the ORIGINAL value of a; for ++a, it is the incremented value of a. In either case, a ends up 1 more than it was originally. For example, if a = 10 and b = 0

    expressiona after expression is evaluatedb after expression is evaluated
    b = a++ + 71117
    b = ++a + 71118

    Using parentheses does NOT change how the evaluation proceeds, so

    expressiona after expression is evaluatedb after expression is e valuated
    b = ( a++ ) + 71117
    b = ( ++a ) + 71118

    Here is a little program you can compile and run for yourself to verify this. (If you'd like to download it, hold the shift key down while you left click!)

    Now having said all of that, I would encourage you NOT to use this construct EXCEPT possibly in loops, because it is a bit tricky and hides what is going on from a human reader of the program. For the human reader, what we have is an unexpected (perhaps) side effect! After all, you can clearly specify what you mean by a++ either by using a = a + 1; or a += 1; If you want the strange side effects to happen, then code them explicitly. (Incidentally, this construct arose because of the way the hardware can work.)

  23. How would the expression ++ -- X ++ ++ be evaluated?

    It wouldn't!!! The java operators prefix and postfix ++ and -- may ONLY be applied to single variables, not to expressions. So, even x++ ++ is not legal as x++ is an expression!

  24. When you declare an object it is not instantiated (created) until the "new" command is called...Is that right, or am I still backwards?

    You're correct!

  25. I am still slightly confused about the instantiation of an object or an int. Can you instantiate an object and give it certain properties at the same time? Or do you have to give it properties afterwards...I'm not sure exactly how it works...

    int's are NOT instantiated as they are primitive types. Remember that the memory where the VALUE of the int will be stored is allocated at the time the int is declared. For objects, the memory where the VALUES of the object's properties will be stored is not allocated UNTIL the object is instantiated (actually created). All that is allocated at declaration time is a place to hold a REFERENCE to the memory that is allocated at instantiation.

    However, your question about instantiation is a good one! You always will instantiate an object and initiatialize its properties at the same time. How the object will be initialized and with what values is determined by which constructor is use. You can see which constructors are available in the class by looking at the javadoc. Remember that the constructors are the methods that have the same name as the class. For example, take a look at the constructors for the String class. What kinds of information can be provided when a String is instantiated?

  26. We had an example that went like the following:

    Customer clemens, twain;
    clemens = new Customer( );
    twain = clemens;
    this creates a reference in clemens and a reference in twain to the same object
    clemens = A
    twain = A
    A = reference to object1

    If you were now to run

    clemens = new Customer( );
    clemens is referenced to a new object. My question is does the twain reference update as well or is is still referencing the original object? Is it
    clemens = B
    twain = A

    A = reference to object1
    B = reference to object2

    clemens = B
    twain = B

    A = reference to object1
    B = reference to object2

    Good question! clemens and twain would refer to two DIFFERENT objects. twain only gets a copy of the reference that was in clemens AT THE TIME of the assignment. There is no permanent linkage between them.

  27. Does the main method just start the program or does it have a larger part to play?

    It marks the place to begin to execute. This is its primary function! However, depending on how much you make it do, it may serve as the coordinating method that oversees the whole process.

  28. My muddiest point is figuring out how the main method fits into the rest of the program. I think I understand the main class (I am thinking of it as being a command that basically turns the java program into the equivalent of an .exe file.please correct me if I am wrong). But I cannot seem to place the main method. Any help here would be greatly appreciated.

    There is no main class, just a main(String[]) class method. It simply marks (or indicates) the place for the "java" interpreter to START executing. By convention this is the first (and only) method, that the java command is willing to call. So, in terms of a .exe file, it would contain the first instructions to execute.

  29. I'm a little lost when it comes to Strings and the way they figure into the programming language.

    Strings are week 3's topic, so I hope that they will be infinitely clearer then! :-)

    I think you can probably envision that a program that a bank would run would need to have access to things like your name and address as well as your account number. Strings are Java objects that can contain sequences of characters (char) in their memory locations. Remember that a variable that is a char can only hold a SINGLE letter. It would be difficult to organize all the individual letters that make up one's name and address using only char variables. Using Strings allow you to treat a sequence of char's as a single entity.

    Questions in this section below this point may prove overwhelming to beginners!

  30. Because boolean variables only have two value: true or false. I think one bit memory is ok for one boolean variable. However by some books, Java boolean variable has one byte for it. it is just a waste of the memory. Can you tell me why?

    I believe the trade-off here is between ease of access to the value and number of bits used. It is inefficient for the machine hardware to get to a single bit. It is cheaper to waste a few bits but to access the boolean value much more quickly.

  31. Can you tell me how the "<<<" operator works?

    I think you mean the >>> operator, because <<< doesn't exist in Java! Both >> and >>> are right shift operators that apply to integral operands (byte, short, int, long). For the purposes of shifting, the operator looks at the memory location as a series of bits rather than as byte, short, int or long. BUT, the first (leftmost) bit of these types of variables indicates whether the number is positive (0) or negative(1). The >> operator shifts each bit in the word right one bit, but leaving the sign bit whatever it was. This effectively divides the value by 2 for all integral values. The >>> operator however fills the "empty spot" left by the sign bit with a zero, thus dividing by 2 for only positive integral values.

  32. How are the float and double type numbers stored in memory? What is the difference in their storage as compared to the integers?

    Checkout section 2.2 of Professor Axel Schreiner's Java course notes:


Muddiest Points from Lab Material

  1. Can you use notepad as a text editor?

    Do you mean, can you use it to edit a java file? Certainly. The advantage though of using emacs the way we have it set up is that you can do compilation and execution from within emacs rather than from a DOS command window!

  2. If the JDK platform is free, does a programmer who makes a profit from a java program have to pay any sort of royalties to Sun Microsystems?


  3. I think the hardest part about lab this week was the wildcards and trying to figure out the shortest command.

    Yes, finding the shortest is tricky, BUT the point really is to learn HOW to use wild card characters to save yourself some typing! So remember that '*' can stand for any number of characters, including none, while '?' stands in for any single character. (It should be noted, however, that '*' cannot stand for a leading period or ANY '/', i.e., directory structure!)

Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
September 15, 2004