No, just the opposite! // means do NOT execute this line as a command!
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.javaor 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 Xxxxor have emacs run our program.
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!
So, what are you going to do to change this?
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!
<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 =.
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.
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.
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.
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.
Look here for detailed information: http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html
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;.
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.
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! :-)
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!
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.
There's certainly is a larger number of bits, but there are two problems:
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.
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.
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!
Perhaps it would help think about these concepts in two pieces:
The expression
a++;
is the same as saying
a = a + 1;
as is the expression
++a;
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
| expression | a after expression is evaluated | b after expression is evaluated |
| b = a++ + 7 | 11 | 17 |
| b = ++a + 7 | 11 | 18 |
Using parentheses does NOT change how the evaluation proceeds, so
| expression | a after expression is evaluated | b after expression is e valuated |
| b = ( a++ ) + 7 | 11 | 17 |
| b = ( ++a ) + 7 | 11 | 18 |
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.)
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!
You're correct!
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?
Customer clemens, twain;this creates a reference in clemens and a reference in twain to the same object
clemens = new Customer( );
twain = clemens;
clemens = AA = reference to object1
twain = A
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 = Bor
twain = AA = reference to object1
B = reference to object2
clemens = B
twain = BA = 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.
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.
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.
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.
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.
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.
Checkout section 2.2 of Professor Axel Schreiner's Java course notes:
http://www.cs.rit.edu/~ats/java-2000-1/
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!
NO!
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!)