Java
  Home arrow Java arrow Page 3 - Completing the Syntactic Comparison of Java and C/C++
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Completing the Syntactic Comparison of Java and C/C++
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-11-28


    Table of Contents:
  • Completing the Syntactic Comparison of Java and C/C++
  • Classes continued
  • Other Distinctions
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Completing the Syntactic Comparison of Java and C/C++ - Other Distinctions
    ( Page 3 of 4 )

    In my previous article I mentioned that the developers of Java constantly focused on portability (being able to run the code on various platforms). C and C++ are programming languages that allow heavy usage of preprocessors. A preprocessor changes the mechanism of how a program runs; it defines constants, includes additional source files (header files), specifies compilation particulars, and so forth.

    Preprocessors are certainly very useful and any C-coder knows that. However, they can be a source of many problems, especially when considering portability. You may be faced with a #define that changes a variable throughout the whole source code, for example, or an #include that adds header files that are required, leaving you with problems if they aren’t reachable, and many other issues.

    Therefore, there are absolutely no preprocessors in Java. Developers considered them to be a hit on cross-platform (and -compiler) portability and thus implemented other workarounds. In Java you do not #include a header file to gain access to its constants and functions, because you import what you need.

    Imports fold the required constants, functions, etc. into your code instead of the header files as a whole. Should you need to define a constant as you did “#define A 50” in C, you approach the situation differently in Java. There is a Globals class (in Java everything is inside classes, remember?) that holds your public constants and static methods. In that class you’d write: “public static final int A = 50;” (without the quotation marks).

    Since there are no preprocessors the usage of #if and #ifdef isn’t allowed either. Conditional compiling is most often used for cross-platform portability and thus isn’t required in Java, but it can be used for debugging or to create particular interfaces for specific platforms. It works a tad bit differently and so it is beyond this article’s scope.

    I’ve recently mentioned constants. In Java each variable that’s declared final is a constant. Let’s consider the following example. We’re going to define Pi as: “public static double PI = 3.14159;”. This type of declaration does not prevent changing its value. Literally we could change its value to 1000. Needless to say, we want a constant and that’s why we’re going to declare Pi as: “public static final double PI = 3.14159;”.

    Speaking of Pi we shouldn’t forget that it’s already declared in the java.lang.Math class. By importing that class you will be allowed to use it throughout your code as Math.PI, not necessarily typing its whole name. Allowing shorter names is a really useful feature.

    Moving on… another deviation from good old C and C++ is related to goto. In Java goto isn’t allowed. However, it is a reserved name but it is not implemented. The workaround to unconditional jumps is using labeled breaks and continues. The aforementioned two work exactly as they do in C/C++ but additionally you can link them with labels. For example: “break label1;” or “continue label2;”.

    Java is a multithreading programming language. Consequently, it has a feature that can be used to prevent simultaneous access to a particular “crucial block of code” from multiple threads; it maintains memory consistency. The keyword is synchronized. Don’t forget that constructors cannot be synchronized (you'd get a syntax error) because it’d be worthless; a constructor is accessed only by the single thread that created it.



     
     
    >>> More Java Articles          >>> More By Barzan "Tony" Antal
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek