syntax differences between c++ and java

Upload: hr

Post on 15-Oct-2015

10 views

Category:

Documents


0 download

DESCRIPTION

All that you want

TRANSCRIPT

  • 5/26/2014 Syntax Differences Between C++ And Java

    http://efytimes.com/e1/fullnews.asp?edid=124731 1/5

    | Electronics | Solar | Consumer Electronics | Infotech | Linux & Open Source | Smartphones |

    Tablets

    | MWC 2014 | EFY Expo 2014

    Monday, May 26, 2014 Select Language

    Search

    Home >> Infotech >> Features

    Syntax Differences Between C++ And Java

    Syntactical differences between Java and C++ are small, but important. Itcan become difficult for a coder, especially when they are moving from C++to Java.

    Rate this news: (12 Votes)

    Friday, December 20, 2013: Are you having a tough timedifferentiating between C++ and Java? Do you keep using thesemicolon when you're not supposed to? While the twolanguages are similar, there are certain syntactical differencesthat can be confusing. Heres something that will come inhandy.

    The main function

    C++

    int main( int X, char* Y[]){printf( "Hello, world" );}

    Java

    Every function in Java has to be made a part of a class. So, the main function also hasto be a part of a class. Moreover, in Java there is one main() function for every class.This can come in handy when writing unit tests for the class.

    class HelloWorld{public static void main(String X[]){System.out.println( "Hello, World" );}}

    Compiling

    C++

    In C++, you will be compiling as,

    g++ X.cc -o outfile

    This will then be run with,

    ./outfile

    Java

    In Java, you will compile the classes in X.java.

    javac X.java

    You have to run this by invoking the static main method.

    SUBSCRIBE TO EFYTIMESReceive the latest reviews, how-tos, news & more.

    Enter your email address:

    Subscribe

    Delivered by FeedBurner

  • 5/26/2014 Syntax Differences Between C++ And Java

    http://efytimes.com/e1/fullnews.asp?edid=124731 2/5

    Daily Weekly Overall

    Class declarations

    While C++ requires a semicolon at the end of class declarations, Java does not haveany such requirement.

    C++

    class X{};

    Java

    class X{}

    Method declarations

    A method declaration in Java must be a part of a class always. Otherwise, both thelanguages are quite the same syntactically on this front. You can also use the public,private and protected access specifications in Java.

    Constructors and destructors

    In the case of constructors, the syntax in both C++ and Java is the same. Java thoughhas no exact replacement for destructors.

    Static members

    Static members (variables and functions) are also declared in the same way in both thelanguages. But, Java allows for static initialisation blocks in order to initialise staticvariables.

    class Foo {static private int x;// static initialization block{ x = 5; }}

    Scoop static namespaces and methods

    C++

    In C++, you will use the Class::method form in order to scoop static methods.

    class MyClass{public:static do();};

    Use this by,

    MyClass::do();

    Java

    In Java, on the other hand, scooping comes with the use of the .again parameter. Thisis similar to accessing the fields of a class.

    class MyClass{public static do(){// do something}}

    To use the static method.

    MyClass.doStuff();

    Object declarationsC++

    // on the stackmyClass x;

    // or on the heapmyClass *x = new myClass;

    Accessing fields from various objects

    C++

    In order to access fields from classes and other such objects, the programmer has touse dots.

    myClass x;x.field;

    If a pointed is involved, then the narrow operator (->) has to be used.

    Most popular

    Features

    Here Are 8 Of The Best Tools For JavaDevelopers!Build some awesome apps for the Android OS

    using these tools exclusively meant for Javadevelopment!...

    PHP Developers Rejoice! Here Are 8 Top

    Productivity Tools For You!The following is an assorted collection of goodproductivity tools for PHP developers!...

    The Top 10 Companies Ruling The Internet Of

    ThingsThe world today is full of a number of devicesconnected to each other. That is what makes the

    internet of things....

    10 Super Useful Linux How Tos For BeginnersAnd Advanced Users Alike!Linux how to guides are the best ways to learn.

    Period! ...

    Want To Be Well Versed In Tcl/Tk? Try These 9Tutorials

    Want to be a part of the millions in the Tcldeveloper community? Here's what you need!...

    Make Your Android Smartphone Awesome WithThese 8 Cool Widgets!

    Widgets are an important part of every Android-powered device. Here are 8 good ones you mustgrab!...

    Movie Buffs Rejoice! Here Are 8 Top Android

    Apps For You!Whether it's that great movie or a hit TV show, yoursmartphone could very well be your handy cinema

    hall/ TV. ...

    And The Top 8 Android Spy Apps Are...The following is an assorted list of good spy appsmeant for the Android platform....

    Here Are 8 Wild Android Hacks!Go beyond conventions and have some fun withyour Android device! ...

    7 Places That Beginner Hackers Will Find Very

    Useful!Looking for the best hacking guides? Maybethese will start you off!...

    Smartphone Review: Samsung Galaxy S5

    While it is a very powerful device, with what isperhaps the best screen till date, Samsung onceagain fails to inspire on the design front....

    Top Editors For Programmers Using Mac PCs!

    Editors make for one of the most basic tools forprogrammers. ...

    Here Are 8 Of The Best Open Source FirewallDistros!

    The following is a list of some of the better opensource firewall distros intended for security

  • 5/26/2014 Syntax Differences Between C++ And Java

    http://efytimes.com/e1/fullnews.asp?edid=124731 3/5

    myClass x = new MyClass();x->field;

    Java

    In Java, only the dot is used. Since we always use references in Java, even pointersare accessed using the dot.

    myClass x = new MyClass();x.field;

    Protection levels

    They are specified in a different manner.

    C++

    public:void X();void Y();

    Java

    public void X();public void Y();

    Virtual functions

    C++

    virtual int X();

    Virtual functions can also be used non virtually. You can simply say into X().

    Java

    int foo(); // or, final int foo();

    In Java, functions are always virtual by default. You use final in order to avoid themfrom being overridden.

    Abstract Classes

    C++

    All you need to do is include public virtual functions./

    class X{ public: virtual void X() = 0; };

    Java

    In Java the syntax allows the programmer to be explicit.

    abstract class X{ public abstract void X(); }

    If you want to specify an interface, then say,interface X{ public void foo(); }

    In that case though, you will have to implement it by,class Y implements X{public void M() { /* do something */ }}

    Memory management

    This is also pretty much the same in both the languages, except that Java has garbagecollection, so no delete for Java.

    Print Email Post Comment (Total Views: 19461)

    Tweet

    StumbleUpon

    Submit

    Infotech News

    Here Are 8 Of The Best Tools For Java Developers!PHP Developers Rejoice! Here Are 8 Top Productivity Tools For You!New Update Brings Kids Mode, Knox 2.0 To Samsung Galaxy S4: Reports

    Dell Regains Number One Spot In Indian PC Market In Q1 2014Samsung Holds On To Flip-Phone Concept, Unveils New 'Master' Feature Phone

    purposes....

    Want To Write Better Java Code? Try These

    Tools!

    Writing good code is often the difference betweena software that is liked and one that is ignored....

    Do You Know These 8 Linux Find Commands?The following is a list of 8 practical operations that

    you can carry out using the Find command....

    Dialogue

    HTC Is Strong And There Are No Plans

    Of Sale Now Or In Future, Says HTC's

    Senior Director-MarketingAtithya Amaresh from EFYTimes had an

    exclusive chat with Sirpa H. Ikola, senior

    director, Marketing, South Asia, HTC

    about its devices and its plans w...

    Cloud And Hybrid Hosting Are The WayTo Go!

    Diksha P Gupta from Open Source For

    You spoke to Anil Chandaliya, chief

    network administrator, ESDS, about howcustomers can play safe while dealing

    w...

    News Powered by PRNewsWire

    MUMBAI, Sunday,

    May 25, 7:46 AM

    Roltas Nine Months Consolidated

    Revenue Grows 59.7% and NetProfit 37.2%

    PUNE, India,

    Sunday, May 25,

    7:31 AM

    Sterlite Grid wins sixth UMTP

    project

    PUNE, India, Friday,

    May 23, 7:08 AMAnibrain School of Media Design,

    Pune's First Digital Design

    Education Destination Launched

    MUMBAI, Friday,

    May 23, 5:23 AMGlobalSpace TechnologiesIntroduces GloEDGE Platform forthe Pharma Industry on Windows

    Embedded OS

    NEW DELHI,

    Friday, May 23, 5:03

    AM

    NCTA Cable TV Industry Wishlist

    for the New I&B Minister

    COPENHAGEN,

    Denmark, Thursday,

    May 22, 10:40 AM

    Milestone Systems ReleasesXProtect 2014

    CHANDIGARH,

    India, Thursday,

    May 22, 7:43 AM

    WizIQ Unveils Online LearningMarketplace; Offers Live

    Instructor-led Courses

    More News

    Featured Resources:

    Apple iPad (New for iOS 7) -- Free Quick Reference

    Card

    This Apple iPad (New for iOS 7) Reference Card provides

    shortcuts, tips, and tricks for the popular tablet device.

    2.1k

    Share

    Java News Java to C++ Int C++ C++ Syntax

  • 5/26/2014 Syntax Differences Between C++ And Java

    http://efytimes.com/e1/fullnews.asp?edid=124731 4/5

    Priv acy

    Press Release

    ViXS Leading UltraHD 4K HEVC Decoder ...

    Centrify Announces $42 Million In ...

    The FIDO Alliance Welcomes Visa To The ...

    Sabtech Appoints Seasoned Aerospace ...

    Premier Holding Corporation Announces ...

    Eguana Technologies Completes ...

    QuickLogic Appoints Rajiv Jain, Vice ...

    Fortinet Invests In Enterprise Identity ...

    Bradley L. Hansen Named ZBB President ...

    Carbon Design Systems And ARM Extend ...

    Vishay Intertechnology To Exhibit ...

    Hitachi Data Systems Federal Announces ...

    SmartMetric Says Vulnerability Of Chip ...

    Linux Foundation Announces Renesas ...

    QuickLogic Taps Shinko Shoji ...

    CTIA Elects Ingram Micro Mobility's ...

    Buy New iPad By Exchanging Old Tablet, ...

    Positive Buyer Response At Asia's ...

    E-Waste Systems Inc. Announces Record ...

    SmartMetric Is Moving Forward Into ...

    PacketVideo's Twonky Extends The ...

    AVAD And Ingram Micro To Present The ...

    Leading U.S. Developer Of Low Cost Next ...

    PasswordBox Wins 2014 Appy Award For ...

    Launch Of Smart24x7 Mobile Platform In ...

    MAIT Reaches Out To The ICT Think Tank ...

    EduKart Partners With Symbiosis Center ...

    Wearable Alert Device From VSN Mobil Is ...

    SMART Modular Introduces DDR3 8GB Ultra ...

    iCrossing Collaboratory Hosts Clients ...

    Newegg Obtains First Federal Circuit ...

    TiVo Reports Results For The First ...

    IEC Electronics Promotes Michael T. ...

    ROI Concepts (ROIC) Is Continuing To ...

    Siemens Museum For Medical Technology ...

    The Vomo ST Helps Wired Devices Enjoy ...

    ColdQuanta Announces New Suite Of ...

    WMNF 88.5 FM Keeps Over 13,000 Pounds ...

    New Smartphone App Takes Away The ...

    New HDMI TO CVBS Converters From China ...

    zBoost Now Shipping zBoost SOHO & ...

    Robust Demand From End-Use Industries ...

    Technology Advancements To Drive The ...

    3D Sensor Market Worth $3,438.25 ...

    "AUTOMATION Is The Biggest Trade Fair ...

    Larson Electronics Reveals New Class 1 ...

    E-Commerce 4 IM Launches Website ...

    Particle Measuring Systems Takes High ...

    Use it to brush up on the basics and to f ind alternate

    methods. >>

    The Bot Threat

    Some of the most serious threats netw orks face today

    are bots, remotely controlled robotic programs that strike

    in many different w ays and deliver destructive payloads,

    self-propagating to infect more and more systems and

    eventually forming a botnet. >>

    Videos

    First Look: LG Optimus GThe phone sports a high-end display and

    comes powered by a powerful processor....

    Create QR-Codes For FreeTEC-IT releases the freeware QR-Code

    Studio to provide a quick and convenientway of QR code creation for everyapplication scenario....

    MWC 2014

    MWC 2014: Tablet Lets People FeelTextures On Its Screen

    Now feel what you see on your tablet, byway of ultrasonic waves....

    MWC 2014: 4K Android Tablet Games To

    Kill Consoles, iPad

    Tablet makers like Samsung want to beat

    the iPad by making 4K tabs. ...

    EFY India

    11,976 people like EFY India.

    Facebook social plugin

    Like

    Comments

  • 5/26/2014 Syntax Differences Between C++ And Java

    http://efytimes.com/e1/fullnews.asp?edid=124731 5/5

    Success Of CONNECTIONS: The Premier ... Cross County, Ark., Begins E-Recording ...

    Manish Kannojiya said: "is there any job for last sem ..."

    on Here Are 10 IT Jobs With The'Best' ... '

    ajay suryawanshi said: "thnx fr this useful info....." on

    Here's How You Can Book An IRCTC ... '

    Events

    19th Consumer Electronic Imaging Fair To Be Held On ...

    home archives contact us advertise with us

    Magazines Portals Directories Events News Verticals Educational Institute Electronics for You

    Open Source for You

    Facts for You

    Electronics Bazaar

    electronicsforu.com

    efytimes.com

    bpotimes.com

    linuxforu.com

    Electronics Annual Guide EFY EXPO

    EFY Aw ards

    EduTech Expo

    OSIDAYS Expo

    Electronics

    Infotech

    Linux & Open Source

    Consumer Electronics

    Science & Technology

    BPO

    EFY Techcenter

    Copyright 2014 EFY Enterprises Pvt. Ltd.

    All rights reserved. Reproduction in whole or in part in any form or medium without written permission is prohibited.

    Usage of the content from the web site is subject to Terms and Conditions

    For Storage Developers