By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Structs / Records in Java

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
Ok, thats clear. Thanks!

Rick
Jul 17 '05 #3

P: n/a


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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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.