Sunteți pe pagina 1din 5

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. It
can become difficult for a coder, especially when they are moving from C++
to Java.

Rate thi s news: (12 Votes)
Friday, December 20, 2013: Are you having a tough time
differentiating between C++ and Java? Do you keep using the
semicolon when you're not supposed to? While the two
languages are similar, there are certain syntactical differences
that can be confusing. Heres something that will come in
handy.
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 has
to 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 EFYTIMES
Receive 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 have
any 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 the
languages 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 though
has no exact replacement for destructors.
Static members
Static members (variables and functions) are also declared in the same way in both the
languages. But, Java allows for static initialisation blocks in order to initialise static
variables.
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. This
is similar to accessing the fields of a class.
class MyClass
{
public static do()
{
// do something
}
}
To use the static method.
MyClass.doStuff();
Object declarations
C++
// on the stack
myClass x;
// or on the heap
myClass *x = new myClass;
Accessing fields from various objects
C++
In order to access fields from classes and other such objects, the programmer has to
use 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 Java
Developers!
Build some awesome apps for the Android OS
using these tools exclusively meant for Java
development!...
PHP Developers Rejoice! Here Are 8 Top
Productivity Tools For You!
The following is an assorted collection of good
productivity tools for PHP developers!...
The Top 10 Companies Ruling The Internet Of
Things
The world today is full of a number of devices
connected to each other. That is what makes the
internet of things....
10 Super Useful Linux How Tos For Beginners
And Advanced Users Alike!
Linux how to guides are the best ways to learn.
Period! ...
Want To Be Well Versed In Tcl/Tk? Try These 9
Tutorials
Want to be a part of the millions in the Tcl
developer community? Here's what you need!...
Make Your Android Smartphone Awesome With
These 8 Cool Widgets!
Widgets are an important part of every Android-
powered device. Here are 8 good ones you must
grab!...
Movie Buffs Rejoice! Here Are 8 Top Android
Apps For You!
Whether it's that great movie or a hit TV show, your
smartphone 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 apps
meant for the Android platform....
Here Are 8 Wild Android Hacks!
Go beyond conventions and have some fun with
your Android device! ...
7 Places That Beginner Hackers Will Find Very
Useful!
Looking for the best hacking guides? Maybe
these will start you off!...
Smartphone Review: Samsung Galaxy S5
While it is a very powerful device, with what is
perhaps the best screen till date, Samsung once
again fails to inspire on the design front....
Top Editors For Programmers Using Mac PCs!
Editors make for one of the most basic tools for
programmers. ...
Here Are 8 Of The Best Open Source Firewall
Distros!
The following is a list of some of the better open
source 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 pointers
are 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 them
from 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 garbage
collection, so no delete for Java.
Pri nt Emai l 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 2014
Samsung 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 between
a 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-Marketing
Atithya 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 Way
To Go!
Diksha P Gupta from Open Source For
You spoke to Anil Chandaliya, chief
network administrator, ESDS, about how
customers 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 Net
Profit 37.2%
PUNE, India,
Sunday, May 25,
7:31 AM
Sterlite Grid wins sixth UMTP
project
PUNE, India, Friday,
May 23, 7:08 AM
Anibrain School of Media Design,
Pune's First Digital Design
Education Destination Launched
MUMBAI, Friday,
May 23, 5:23 AM
GlobalSpace Technologies
Introduces GloEDGE Platform for
the 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 Releases
XProtect 2014
CHANDIGARH,
India, Thursday,
May 22, 7:43 AM
WizIQ Unveils Online Learning
Marketplace; Offers Live
Instructor-led Courses
More News
Featured Resources:
Apple iPad (New for iOS 7) -- Free Quick Reference
Card
This Apple iPad (New f or iOS 7) Ref erence Card provides
shortcuts, tips, and tricks f or 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 networks f ace today
are bots, remotely controlled robotic programs that strike
in many dif f erent ways and deliver destructive payloads,
self -propagating to inf ect more and more systems and
eventually f orming a botnet. >>
Videos

First Look: LG Optimus G
The phone sports a high-end display and
comes powered by a powerful processor.
...
Create QR-Codes For Free
TEC-IT releases the freeware QR-Code
Studio to provide a quick and convenient
way of QR code creation for every
application scenario....
MWC 2014

MWC 2014: Tablet Lets People Feel
Textures On Its Screen
Now feel what you see on your tablet, by
way 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 f or You
Open Source f or You
Facts f or You
Electronics Bazaar
electronicsf oru.com
ef ytimes.com
bpotimes.com
linuxf oru.com
Electronics Annual Guide EFY EXPO
EFY Awards
EduTech Expo
OSIDAYS Expo
Electronics
Inf otech
Linux & Open Source
Consumer Electronics
Science & Technology
BPO
EFY Techcenter


Copyri ght 2014 EFY Enterpri ses Pvt. Ltd.
Al l ri ghts reserved. Reproducti on i n whol e or i n part i n any form or medi um wi thout wri tten permi ssi on i s prohi bi ted.
Usage of the content from the web si te i s subj ect to Terms and Condi ti ons
For Storage Developers

S-ar putea să vă placă și