Berkeley CSUA MOTD:Entry 12687
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2004/3/15-16 [Computer/SW/Languages/Java] UID:12687 Activity:nil
3/15    ***YAWN*** can someone please post an interesting/mind boggling
        c/java trivia? Like if "I have class A {int x; } class B extends {...}
        what is the expected behaviour?" Something short but interesting
        for discussion.
        \_ Implement the factorial function without iteration or named
           functions (using lambdas is ok).  Dave, you aren't allowed to
           answer this one.  Any language is allowed.  For bonus points,
           do it in a language which infers types for you.  -- ilyas
        \_ http://www.gotw.ca/gotw/005.htm
        \_ if you're bored, go read the comp.lang.c FAQ
           http://www.eskimo.com/~scs/C-faq/top.html or the Java IAQ
           http://www.norvig.com/java-iaq.html
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2013/4/29-5/18 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:54665 Activity:nil
4/29    Why were C and Java designed to require "break;" statements for a
        "case" section to terminate rather than falling-through to the next
        section?  99% of the time poeple want a "case" section to terminate.
        In fact some compilers issue warning if there is no "break;" statement
        in a "case" section.  Why not just design the languages to have
        termination as the default behavior, and provide a "fallthru;"
	...
2013/5/1-18 [Computer/SW/Languages/Java, Computer/Theory] UID:54669 Activity:nil
5/1     What's the difference between CS and Computer Engineering?
        http://holykaw.alltop.com/top-ten-paying-degrees-for-college-graduates
        \_ One is science and the other is engineering.
        \_ From http://en.wikiquote.org/wiki/Computer_science
           'A folkloric quotation ... states that "computer science is no more
           about computers than astronomy is about telescopes."  The design
	...
2013/3/5-26 [Computer/SW/Languages/Java] UID:54618 Activity:nil
3/5     Three emergency Java updates in a month. Why do I have a feeling
        that the third one won't be the last one?
        \_ Bingo!
	...
2012/12/18-2013/1/24 [Computer/SW/Languages/Perl] UID:54561 Activity:nil
12/18   Happy 25th birthday Perl, and FUCK YOU Larry Wall for fucking up
        the computer science formalism that sets back compilers development
        back for at least a decade:
        http://techcrunch.com/2012/12/18/print-happy-25th-birthday-perl
        \_ I tried to learn Perl but was scared away by it.  Maybe scripting
           lanauages have to be like that in order to work well?
	...
2012/12/4-18 [Computer/SW/Languages/Java] UID:54544 Activity:nil
12/4    Holy cow, everyone around me in Silicon Valley is way beyond
        middle class according to Chinni's definition:
        http://en.wikipedia.org/wiki/American_middle_class
        \_ Let's set our goals higher:
           http://en.wikipedia.org/wiki/Upper_middle_class_in_the_United_States
           \_ How about this one?
	...
2012/10/29-12/4 [Science/Disaster, Computer/SW/Languages/Java, Politics/Domestic/President/Bush] UID:54516 Activity:nil
10/29   Go Away Sandy.
        \_ Sorry, Coursera is performing preventive maintenance for this
           class site ahead of Hurricane Sandy. Please check back in 15 minutes.
           class site ahead of Hurricane Sandy. Please check back in 15
           minutes.
        \_ Bitch.
	...
Cache (2134 bytes)
www.gotw.ca/gotw/005.htm
This is the original GotW problem and solution substantially as posted to Usenet. See the book Exceptional C Addison-Wesley, 2000 for the most current solutions to GotW issues 1-30. The solutions in the book have been revised and expanded since their initial appearance in GotW. The book versions also incorporate corrections, new material, and conformance to the final ANSI/ISO C standard. Overriding Virtual Functions Difficulty: 6 / 10 Virtual functions are a pretty basic feature, right? If you can answer questions like this one, then you know them cold. Problem In your travels through the dusty corners of your companys code archives, you come across the following program fragment written by an unknown programmer. The programmer seems to have been experimenting to see how some C features worked. What did the programmer probably expect the program to print, but what is the actual result? This distinction is very important, because it means that Base::fint and Base::fdouble are not visible in Derived! Note that certain popular compilers do not even emit a warning for this. RULE When providing a function with the same name as an inherited function, be sure to bring the inherited functions into scope with a using declaration if you dont want to hide them. Derived::g int i 10 Unless youre really out to confuse people, dont change the default parameters of the inherited functions you override. In general, its not a bad idea to prefer overloading to parameter defaulting anyway, but thats a subject in itself. Yes, this is legal C, and yes, the result is well-defined, and no, dont do it. RULE Never change the default parameters of overridden inherited functions. Now that we have the major style issues out of the way, lets look at the mainline and see whether it does that the programmer intended: void main Base b; The thing to remember is that, like overloads, default parameters are taken from the static type here Base of the object, hence the default value of 10 is taken. However, the function happens to be virtual, and so the function actually called is based on the dynamic type here Derived of the object.
Cache (1617 bytes)
www.eskimo.com/~scs/C-faq/top.html -> www.eskimo.com/%7Escs/C-faq/top.html
Content from the book C Programming FAQs: Frequently Asked Questions Addison-Wesley, 1995, ISBN 0-201-84519-9 is made available here by permission of the author and the publisher as a service to the community. It is intended to complement the use of the published text and is protected by international copyright laws. The content is made available here and may be accessed freely for personal use but may not be published or retransmitted without written permission. An FAQ list is a collection of questions commonly asked on Usenet, together with presumably definitive answers, provided in an attempt to keep repeated questions on the newsgroup down to a low background drone so that discussion can move on to more interesting matters. Since they distill knowledge gleaned from many sources and answer questions which are demonstrably Frequent, FAQ lists serve as useful references outside of their originating Usenet newsgroups. This list is, I dare to claim, no exception, and the HTML version youre looking at now, as well as other versions referenced just below, are intended to be useful to C programmers everywhere. Several other versions of this FAQ list are available, including a book-length version published by Addison-Wesley . I would, of course, like it if it were perfect, but its been two years or so since I first started talking about putting this thing on the web, and if I were to wait until all the glitches were worked out, you might never see it. Each page includes a mail feedback button, so you can help me debug it. At first, you dont have to worry about reporting minor formatting hiccups;
Cache (8192 bytes)
www.norvig.com/java-iaq.html
There are lots of Java FAQs around, but this is the only Java IAQ. There are a few Infrequently Asked Questions lists, including a satirical one on C . Q: The code in a finally clause will never fail to execute, right? But heres an example where the finally code will not execute, regardless of the value of the boolean choice : try if choice while true ; Anytime you define a equals method, you should also define a hashCode method. You must make sure that two equal objects have the same hashCode, and if you want better hashtable performance, you should try to make most non-equal objects have different hashCodes. Some classes cache the hash code in a private slot of an object, so that it need be computed only once. The documentation in Hashtable talks about entries in the table, and the behavior of Properties is consistent if you assume that defauls are not entries in the table. If for some reason you thought defaults were entries as you might be led to believe by the behavior of getProperty then you will be confused. The previous two questions show that a programmer neeeds to be very careful when extending a class, and sometimes just in using a class that extends another class. Problems like these two lead John Ousterhout to say Implementation inheritance causes the same intertwining and brittleness that have been observed when goto statements are overused. As a result, OO systems often suffer from complexity and lack of reuse. Scripting , IEEE Computer, March 1998 and Edsger Dijkstra to allegedly say Object-oriented programming is an exceptionally bad idea which could only have originated in California. You need to pay as much attention to the methods that you dont over-ride as the methods that you do. This is one of the big fallacies of Object-Oriented design using inheritance. Youre especially looking for trouble if the subclass changes the contract of any of the methods, or of the class as a whole. It is difficult to tell when a contract is changed, since contracts are informal there is a formal part in the type signature, but the rest appears only in comments. In the Properties example, it is not clear if a contract is being broken, because it is not clear if the defaults are to be considered entries in the table or not. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesnt force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use because it is not a subclass. For the HashtableWithPlurals example, delegation would give you this note: as of JDK 12, Dictionary is considered obsolete; The Properties example, if you wanted to enforce the interpretation that default values are entries, would be better done with delegation. Because the Java implementation team was rushed, and took the course that required writing less code. Global variables are considered bad form for a variety of reasons: Adding state variables breaks referential transparency you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables. State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state. When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once. In each case, you need to decide two things: how many copies of this so-called global variable do I need? Here are some common solutions: If you really want only one copy per each time a user invokes Java by starting up a Java virtual machine, then you probably want a static instance variable. For example, you have a MainWindow class in your application, and you want to count the number of windows that the user has opened, and initiate the Really quit? You could easily add an instance variable to the Browser class to hold the display stream or frame, but you havent passed the current instance of the browser down into the methods in the Cookie class. You dont want to change the signatures of many methods to pass the browser along. You cant use a static variable, because there might be multiple browsers running. Peter van der Linden , author of Just Java , recommends against both of the last two practices in his FAQ . I agree with him that Math m null is a bad idea in most cases, but Im not convinced that the MyStaticMethods demonstrates very poor OOP style to use inheritance to obtain a trivial name abbreviation rather than to express a type hierarchy. See an example of how I used this approach to what I thought was good effect. Second, it is rather presumptuous to say that this is very bad OOP style. You could make a case that it is bad Java style, but in languages with multiple inheritance, this idiom would be more acceptable. Another way of looking at it is that features of Java and any language necessarily involve trade-offs, and conflate many issues. I agree it is bad to use inheritance in such a way that you mislead the user into thinking that MyClass1 is inheriting behavior from MyStaticMethods , and it is bad to prohibit MyClass1 from extending whatever other class it really wants to extend. But in Java the class is also the unit of encapsulation, compilation mostly, and name scope. The MyStaticMethod approach scores negative points on the type hierarchy front, but positive points on the name scope front. If you say that the type hierarchy view is more important, I wont argue with you. But I will argue if you think of a class as doing only one thing, rather than many things at once, and if you think of style guides as absolute rather than as trade-offs. There are at least three different meanings that null is commonly used to express: Uninitialized. For example, terminal nodes in a binary tree might be represented by a regular node with null child pointers. Note that this is subtly different from the previous case, although some people make the mistake of confusing the two cases. The difference is whether null is an acceptable tree node, or whether it is a signal to not treat the value as a tree node. Compare the following three implementations of binary tree nodes with an in-order print method: // null means not applicable // There is no empty tree. This method is not foolproof, because a garbage collection could occur in the middle of the code you are instrumenting, throwing off the byte count. Also, if you are using a just-in-time compiler, some bytes may come from generating code. You might be surprised to find that an Object takes 16 bytes, or 4 words, in the Sun JDK VM. This breaks down as follows: There is a two-word header, where one word is a pointer to the objects class, and the other points to the instance variables. Even though Object has no instance variables, Java still allocates one word for the variables. Finally, there is a handle, which is another pointer to the two-word header. Sun says that this extra level of indirection makes garbage collection simpler. There have been high performance Lisp and Smalltalk garbage collectors that do not use the extra level for at least 15 years. I have heard but have not confirmed that the Microsoft JVM does not have the extra level of indirection. An empty new String takes 40 bytes, or 10 words: 3 words of pointer overhead, 3 words for the instance variables the start index, end index, and character array, and 4 words for the empty char array. Creating a substring of an existing string takes only 6 words, because the char array is shared. Putting an Integer key and Integer value into a Hashtable takes 64 bytes in addition to the four bytes that were pre-allocated in the Hashtabl...