|
-
May 2nd, 2002, 12:44 AM
#1
Thread Starter
Dazed Member
Structures?
Does anyone know off-hand if java allows the creation UDT's or structures similar to what Visual Basic 6 or Vb.Net has now? Im going to say no because ive never seen a structure declared in java code but maybe someone else has.
-
May 2nd, 2002, 03:43 AM
#2
-
May 2nd, 2002, 10:34 AM
#3
Frenzied Member
Well, this is a timely thread!
I just started looking into the feasibility of writing a Java socket applet to send structured messages from a web page to C programs running on our Himalaya Non-stop servers. I have done some VB sockets apps that send UDTs to the same type of C programs with no problems, so I know that it is possible to create UDTs that are structurally identical to a C struct.
With limited JavaScript experience and no Java experience, I picked up a copy of Learning Java from O'Reilly. On page 88, it reads "In C, you can make a new, complex data type by creating a struct. In Java (and other object-oriented languages), you instead create a class that defines a new type in the language.".
Does this mean that a class that contains only variables would be identical to a UDT or struct? If I am off base, now would be the time to let me know, before I get too far down the wrong path.
-
May 2nd, 2002, 12:16 PM
#4
Well ...
That rules out the possibility of structures in Java.
UDT can be called pseudo-classes, i.e. very similar to classes but not exactly classes. If Java advises you to use classes in place of UDTs, then it does not support structures.
.
-
May 2nd, 2002, 01:36 PM
#5
Addicted Member
to ccoder...
I don't know whether a class with just variables is the same as a UDT. I don't believe it would (but i'm not sure and can't logically argue it at the mo).
But i have an idea how to get it to work... but it is v complex.
You could get your vb app to send a java app your UDT using sockets and then look at the data with a byte output stream. Try sending different UDT's with different amounts of variables in to see if you can work out the layout your UDT would have to take.
You could then try and recreate it in Java. Perhaps try doing the same but sending objects from a java program to a java program and analysing that in a byte input stream.
It is pretty complex tho, and would require a lot of work, but i believe you could fool the C program into thinking that the bytes you are sending is really a UDT.
Good Luck
Mrs Kensington
Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!
-
May 2nd, 2002, 01:56 PM
#6
Thread Starter
Dazed Member
Well lets first define what the main difference is between a class(which is a refrence type) and a structure(which is a value type) is. For instance an integer or byte has a fixed number of bytes regardless of the actual value held by the variable. So these are normally stored on the stack. For refrence types such as vectors
memeory is allocated dynamically so they are commonly stored on the heap.
As i understand it, the main reason for this seperation is that when a process needs to retrieve a variable it has to search the stack, looking for the variable specified. Now even with complex algorithms the search might take a long perod of time if the stack included large dynamic items such as collections and sets. So a refrence(or pointer) is just stored in a memory address that resides on the stack which points to where the dynamic type resides on the heap.
Last edited by Dilenger4; May 2nd, 2002 at 10:40 PM.
-
May 2nd, 2002, 01:59 PM
#7
Thread Starter
Dazed Member
Posted by ccoder
Does this mean that a class that contains only variables would be identical to a UDT or struct?
I would have to say no.
-
May 2nd, 2002, 03:12 PM
#8
Frenzied Member
Originally posted by Dilenger4
I would have to say no.
I suspected as much. Nothing's ever that easy!
It looks like I started some good dialog though. It should get me pointed in the right direction. I have been kind of thinking that I may have to 'string' my values into some sort of contiguous memory, like a byte array or a byte stream (if it is different) as Mrs Kensington mentioned.
Well, back to the books. I have a lot to learn.
-
May 2nd, 2002, 10:53 PM
#9
Thread Starter
Dazed Member
Posted by ccoder
I suspected as much. Nothing's ever that easy!
You got that right.
-
May 7th, 2002, 03:52 PM
#10
Hyperactive Member
easily, just create a class with every data member beign public and don't give it any methods and there you go
if someone can think of ANY differences then please do tell, hell if you wanted to you could overide all your inherited public methods with private ones couldn't you?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
May 7th, 2002, 11:36 PM
#11
Thread Starter
Dazed Member
posted by CaptainPinko
easily, just create a class with every data member beign public and don't give it any methods and there you go
That wouldn't do. The class would still be stored on the heap not the stack as UDT's or structures are. For instance take following block of VB.Net code.
Code:
'use a decimal refrence type
Dim total as DecimalRef = New DecimalRef()
Dim subtotal as DecimalRef = New DecimalRef()
'calculate the subtotal
'set the total equal to subtotal and apply tax and shipping
total = subtotal
total = total + tax + shipping
'Oops
' we just applied tax and shipping to subtotal as well
The only way around this would be to declare total and subtotal as a value type or the following
Code:
total = subtotal.Clone()
total = tax + shipping
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|