472,127 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Structs / Records in Java

Hi,

We all know that Java has classes but how about basic storage objects like
structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types
etc. How about Java?

Greetings,
Rick

Jul 17 '05 #1
8 25734
Rick wrote:
Hi,

We all know that Java has classes but how about basic storage objects like
structs? C and C++ have Structs, Pascal has Records, Visual Basic has
Types etc. How about Java?


Java does not support structs and records. Use classes instead.

--
Jonas Kongslund
Jul 17 '05 #2
Ok, thats clear. Thanks!

Rick
Jul 17 '05 #3


While it was 3/11/03 10:10 am throughout the UK, Jonas Kongslund
sprinkled little black dots on a white screen, and they fell thus:

<snip>
Java does not support structs and records.

<snip>

Yes it does. It just calls them classes, and greatly expands upon their
functionality.

Even in C++, a struct _is_ a class. There, the struct keyword just
serves for backward compatibility and/or syntactic sugar depending on
your point of view.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 17 '05 #4
"Rick" <as******@hotmail.com> wrote in message news:<3f***********************@news.xs4all.nl>...
Ok, thats clear. Thanks!

Rick


hey
structs in C++ are just classes with default access specifier as
public (otherwise there is no differnec in class in C++ & a struct in
C++)
thus if you need to store only data, java does not force you to have
methods in class
for eg-: while creating data structures you can create class for Node
class Node
{
int data;
Node next;
}
this is equivalent to structs .... isnt it ?

regards
amey
Jul 17 '05 #5
Amey Samant wrote:
class Node
{
int data;
Node next;
}
this is equivalent to structs .... isnt it ?


Nope... default access for instance variables is protected, not public.
Should be:

class Node
{
public int data;
public Node next;
}

/<en

Jul 17 '05 #6
I'm not 100 % sure but I don't think you may say a struct is the same as a
class. Of course, they both can be used for storing data. Classes are some
sort of advanced struct types, based on structs (since OOP languages like
Java and C++ languages came after non-object-orientated languages like C
which already had structs that time). So, they won't differ that much but
it's the same as :
"A lion is a beast but a beast is not a lion." (what a stupid example :| ).

It doesn't matter really much, I can live with the Java classes, but I was
just curious. Or maybe I was lazy because using structs if a little bit
simpler because you don't have to create them like classes.

Greetings,
Rick
Jul 17 '05 #7
Stewart Gordon <sm*******@yahoo.com> wrote in message news:<bo**********@sun-cc204.lut.ac.uk>...

hi stewart
Even in C++, a struct _is_ a class.
There, the struct keyword just
serves for backward compatibility and/or syntactic sugar depending on
your point of view.


no structs in C++ are different than class (the only difference is in
class by default access specifier is private whereas in structs it is
public)
otherwise they are same
regards
amey
Jul 17 '05 #8


While it was 4/11/03 10:01 am throughout the UK, Amey Samant sprinkled
little black dots on a white screen, and they fell thus:

<snip>
no structs in C++ are different than class (the only difference is in
class by default access specifier is private whereas in structs it is
public)

<snip>

I didn't say that the C++ keywords 'struct' and 'class' were synonyms.
I merely meant that by defining a struct, you are defining a class, and
it just saves having to put public: at the top.

Although it isn't enforced by the language, it remains common practice
(IINM) to use 'struct' for C-style structs, and 'class' when you want to
use the functionality of a class.

Basically,

struct Qwert {

means exactly the same as

class Qwert {
public:

and would compile to precisely the same.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Brueck | last post: by
18 posts views Thread by Rick | last post: by
61 posts views Thread by Marty | last post: by
19 posts views Thread by desktop | last post: by
29 posts views Thread by s0suk3 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.