Java man

R

r3coil

Guest
I am 17, took the Java A course at school and passd, ect. I can write recursive methods, and all that. How will i do when it comes to coding C++ and HL? I know a bit C++ but as far as I know its close to java, not the same though. But is it close a enough where i can go, oh yea i know that. And do I know enough about prog. to prog a mod? I can write GUI's and advanced methods, server side stuff, all that junk.
 
Um well I learnt Java after C++ and the Java API is alot easier to learn and so is the way it treats pointers.

C++ is very similiar to Java, but what really matters is how complex the HL2 SDK is. The Java API is very easy to learn in comparison to the windows API. Although I have been told that VC JFC (I think that's what it is called) component library is equally as easy to use or .NET for that matter.

I'm not really sure about the HL SDK haveing never bothered to use it. As long as you can think for yourself and don't need to work from a guide it shouldn't be too hard. You should definately learn C++ first though.

BTW whats a Java A course? I'm doing A level computing and have just been told I can't use Java for my database because the moderator won't understand it. They should rename it to ICT for business.
 
mrchimp, thats ****in shocking, do it in java and if he can't grade it, tough :D
i used java in sys computing, but i didnt have a teacher, so i dropped it and played pool instead :E use it at uni all the time though, you shouldnt have many problems migrating to c++
 
yea i looked at some code last night and the pointers throw me off, but its simaler and the syntax is only hard part about the diffrence. Im in High school and I took a college course AP JAVA, but it was the A, not the AB. AB is the second half of the book we were going through. I cant wait for Java to be used more, its just slower than C++ correct? Because it cant me used to control hardware specs? Thats what i was told
 
saying java is slower then C++ will start a flame war. If you really want to get into that, i suggest doing some research about it. google, slashdot, osnews are good places to start
 
Any questions about java can be answered here: http://forum.java.sun.com/

For inexperienced coders, Java apps are usually a little slower since it takes more knowledge to optimize the code for speed. As for programming for specific OS's, there are times where you might need to use the Java Native Interface. The plus with java is that it is platform independent, but apps are handled by the JVM which causes a little scarifice for speed over c++.
 
Saying java is slower than C++ shouldn't start a flame war since it is slower. All interpreted languages are. This is not to say that C++ is the fastest language... its not, just a lot faster than java. (When getting into the complextity required for graphics/physics/ai engines, it can be 2-10 times as fast). *NOTE* see below for ACTUAL speed tests, not some random person's opinon.

Speed is not the only difference between java and c++:

c++ supports multiple inheritence -> java does not
----- this is just stupid of SUN to not allow multiple inheritence
c++ supports inline assembly -> java does not
----- that would defeat the puprose of it being interpreted at run time
c++ uses pointers -> java has no concept of pointers
----- you can *almost* get around this using references but they won't do for all cases
c++ has explicit memory deallocation -> java has an automatic garbage collecter
---- some people like java's garbage collecter, I can't stand it
c++ is fully extensible -> java is not
---- why SUN did not make java more extensible is beyond me, it is a very useful (though tedious) aspect of c++


Now, with all that in mind here's the part which should stop any arguments about which is better.

Java allows you to import compiled c++ libraries.

So why the hell are any of you deciding to only use ONE of them. THEY HAVE DIFFERENT STRENGTHS... USE THEM BOTH.

You should never limit yourself to the use of a single language. Each language has strengths and weaknesses, use the one that best fits your current problem (unless explicitly told to do otherwise).

--------------------------------------------------------------------------
Here are two repeatable speed comparisons,
1) A slightly outdated test (java 1.1.5, java 1.2, java 1.3 beta used... note 'Java 2' is java 1.4) using a bubble sort with varying data structures. C++ never does worse than twice as fast for like data structures, and using best data structures C++ is incredibly faster.
link :: http://verify.stanford.edu/uli/java_cpp.html

2) A recent test using java 1.4.2 (1.5 beta is the next, and latest, version). This test results in c++ being roughly twice as fast as java.
link :: http://cgi.eaglecentre.force9.co.uk...ives/000314.php
 
Hmm. Akrin, I'm sure you know all or most of this, this is just for reference:

So Java very intentionally doesn't support multiple inheritance. There are a few good reasons for this. The most important (as I see it) is that it would break and confuse Java's dynamic dispatch.

Java's "references" are actually called "handles." The idea, as with many Java features, is to have a very secure language. Preventing the programmer from having direct access to memory locations both reduces that chances that he'll mess that up, and protects against malicious programs.

Garbage collection is another (obviously) very intentional thing. Automatic garbage collection means the programmer spends almost no time searching for memory leaks related to de-allocation. Another point about Java memory is that the JVM will not allocate more memory for a Java app once it's started (unlike a C++ app, say, which can continue to grow). This is another security feature. (The heap size can be adjusted before running).

I'm not sure what Akrin means by not extensible.

A big point with Java is that performance is heavily dependant on the JVM you use. There are many tricks a JVM may use to bring Java performance on par with native code.

With all that said, Akrin is right about being able to and willing to program in more than one language.

(Btw Akrin your last link doesn't work).
 
Of coarse multiple innheritence brings up a whole bunch of problems, a quick example of double inheritence (A inherits from B & C, B&C inherit from D, A inherits D twice and end of the universve follows), but SUN could've found ways to implement it... they didn't. I know it isn't used too often in c++, but it is nonetheless used.

Java's "handles", sorry for referring to them as "references"... hard to keep c++ and java terms seperate when they do the same thing, may prevent malicous programs and programming errors... but from my point of view this is symptomatic of a major problem in computing science. The belief that everything should be made fool proof at the cost of usefulness. ("If you make a system that any fool could use, only fools would want to use it" - one of murphy's laws). You can do things with pointers that can not be done with handles/references.

The inclusion of the garbage collector is much like the exclussion of direct memory access to me. It is weakens the strength of the language to appeal to those who don't understand how to properly manage memory. And it is not infallible, nothing is.

extensible
1. Capable of being extended or protruded: an extensible tongue; extensible tables.
2. Computer Science. Of or relating to a programming language or a system that can be modified by changing or adding features.

C++ is extensible in that you can actually expand the base language (ie. defining new base variable types and commands). Java does not allow you to create new base variable types, you can only create objects that inherit from 'object'.

As for the last link, just copied it from a previous post... copied the text, not the link. Here is the right link http://cgi.eaglecentre.force9.co.uk/blog/archives/000314.php
Thanks for telling me.
 
Where do you get the program to write/compile c++?

I'd like to learn a little of it but I've never got around to trying.
 
If you want the minimum (read: free) to start programming in c++ all you need is notepad (I'm assuming you're using windows) and any number of free compilers, though I would recommend GCC (GNU Comipler Collection) http://gcc.gnu.org/releases.html

However if you start doing a lot of c++ programming you should probably get an IDE (integrated development environment) such as Microsoft Visual C++ (http://www.amazon.com/exec/obidos/A...8893750/sr=2-1/ref=sr_2_1/104-0720378-6843958) or Borland C++ Builder (http://www.amazon.com/exec/obidos/A...8893750/sr=2-2/ref=sr_2_2/104-0720378-6843958).

Also see these threads:
http://www.halflife2.net/forums/showthread.php?t=20491
http://www.halflife2.net/forums/showthread.php?t=26389
http://www.halflife2.net/forums/showthread.php?t=23166
 
C++ is extensible in that you can actually expand the base language (ie. defining new base variable types and commands). Java does not allow you to create new base variable types, you can only create objects that inherit from 'object'.

Just what exactly (in real-life) would you need new base-types for? It's already got every basetype you'll ever need :/
 
hax said:
Just what exactly (in real-life) would you need new base-types for? It's already got every basetype you'll ever need :/

By the same token, why would you need anything other than a one byte variable type? Who needs doubles, floats, longs, ints, shorts when you can interpret chars to do EVERYTHING?

Oh that's right... because its not very convenient to do that.

Similarly its not too convenient to store/manipulate arbitrarily long values with the default base types. Now, you could just have a standard class do this but then any equation using these variables would become cumbersome very quickly. You'll want them to act like any other base type.

Or what about bit-vectors? In both java and c++ booleans still take 1 byte to store 1 bit of information; very innefficient. So instead of going through the pains of using bit-masks so that you can store groups of bits efficiently... wouldn't it be nice to define a new base type that handled this problem without padding your code like using bit-masks or class functions does?

Or perhaps a vector type that allows two vectors mapped in different spaces to be used in the same operation without writing all the code necessary to do the conversion during the equations?

Or maybe a true string object for c++ that acts as if it was a base type? Unless of course you prefer character arrays and using string manipulation functions.

Or maybe you want a base type that acts like int in all regards except that it interprets negative values as false in boolean expressions.

Want more examples?
 
Akrin said:
Or maybe you want a base type that acts like int in all regards except that it interprets negative values as false in boolean expressions.

Want more examples?

Base types and templates are completely different arn't they?

Can you give me a link to a tutorial on the syntax of base types?

On the subject of Java's speed you can compile it to native code if you only use AWT and some other API components or so I heard. Never bothered trying it myself as I don't need to.

After I have finnished my current project I might try compareing the performance of my 2D particle physics engine in C++ to Java. It deosn't do anything that can't be done in Java and apart from the basics like vector classes it doesn't use any external librarys.
 
mrchimp said:
Base types and templates are completely different arn't they?

Yes they are.

mrchimp said:
Can you give me a link to a tutorial on the syntax of base types?

You just need to implement all the operators for the class, the operators that you can define are:

unary operators
! Logical NOT
& Address-of
~ One's complement
* Pointer dereference
+ Unary plus
++ Increment
– Unary negation
–– Decrement
–> Member Access (*operator-> as apposed to operator->)


binary operators
, Comma
!= Inequality
% Modulus
%= Modulus/assignment
& Bitwise AND
&& Logical AND
&= Bitwise AND/assignment
* Multiplication
*= Multiplication/assignment
+ Addition
+= Addition/assignment
– Subtraction
–= Subtraction/assignment
–> Member selection
–>* Pointer-to-member selection
/ Division
/= Division/assignment
< Less than
<< Left shift
<<= Left shift/assignment
<= Less than or equal to
= Assignment
== Equality
> Greater than
>= Greater than or equal to
>> Right shift
>>= Right shift/assignment
^ Exclusive OR
^= Exclusive OR/assignment
| Bitwise inclusive OR
|= Bitwise inclusive OR/assignment
|| Logical OR


trinary operators
As far as I can tell you can't re-define the conditional expression operator.

For unary operators, implementing as a member function takes the form
rettype operator op ( ), implementing as a global function takes the form rettype operator op (arg),

For binary operators, implementing as a member functions takes the form
rettype operator op (arg), implementing as a global function takes the form rettype operator op (arg1, arg2).

You also need to implement all relevent conversion functions. Converting from another type to your object takes the form of a conversion constructor, converting from your type to another takes the form "operator type()" (return type is implicitly defined to be type, you do not have to state it)

Note, you can provide multiple definitions of operators with different arguments. An example where this would be usefull; consider your type can be added to an integer, providing the operator +() for that specific case would preclude a conversion which would save time.

Note 2, if you are doing a global implementation you may need to have the object you are creating treat it as a "friend" to access private data members.

Note 3, implement as many operators as possible as inline functions to reduce unnecessary function calls.
 
You learn something new everyday.

I'm not sure why I made a connection between templates and operator overloading. Although the most usefull thing I could think of doing with it would be to allow vectors and binary trees to be added together or have "greater than" type operations performed on them.

You could just use a normal function though, it would only be really usefull in a big project or the creation of a library.
 
ok, does -> refer to a pointer in C++? Im new to it ect, so it refers to the mem bank of obj1 in ob1 -> ob2? arg
 
-> is a scope resolution operator used for member selection on objects with 1 level of indirection... or incase you wanted that in english....

When you have a pointer to a compound object (class / struct) you can not use the '.' operator to resolve the symbol by itself; this is because pointers to objects don't have member attributes... they're pointers not objects.

(Using a compound variable 'A' with member variable 'B')
So you have to use either (*A).B or A->B, however (*A).B is generally used in C not C++.

You will commonly see this used in the implementation of member functions in conjunction with the 'this' pointer.

Unfortuantly if you have more than 1 level of indirection (ie a pointer to a pointer to a pointer....) then you can only use the -> operator for the last level of indirection. In these cases it is probably a good idea to rethink your design... more than 2 levels of indirection is not usually a sign of good code.
 
well that explains a whole lot! Im used to java ;-) Im so confused still! ok, so why would u have to point point point and not point? Maybe some simple helloWorld skill level examples?
 
here are some examples:
Code:
// Simple
int iData = 12;
int* pData = NULL; // pData point to zero

pData = &iData; // pData points to iData, & is the address operator
*pData = 6; // pData still points to iData, but iData is now 6

// Array
int mArray[5] // array contains 5 elements of type int
int* pArray = &mArray[0]; // Pointer points to the first element of the array

*pArray = 5; // The first element of the array is now 5
pArray++; // Increase the pointer. It now point to the second element
*pArray = 6; // The second element is 6
I hope those examples are usefull, if you want some more advanced examples, just ask.
 
Oh! Well that makes a lot more sense. So where does the -> operand come in?
 
An code example using the -> operator.

Code:
#include <iostream>
using namespace std;

class example_for_r3coil{
public:
	char *s;
	int n;

	example_for_r3coil(char *a=NULL, int x=0):s(a),n(x){};
};

void main()
{
	example_for_r3coil * pExample = new example_for_r3coil("-> example",5);

	cout<<"(*pExample).s = '"<<pExample->s
		<<"' and (*pExample).n = '"<<pExample->n
		<<"'."<<endl;
}

Output = (*pExample).s = '-> example' and (*pExample).n = '5'.
 
k all is good until I hit the code
Code:
	cout<<"(*pExample).s = '"<<pExample->s
		<<"' and (*pExample).n = '"<<pExample->n
		<<"'."<<endl;
the syntax cout does what? But other than that U get the whole pointing thing! C++ isnt much off the lines from java! Just a bit more complex with memory ect.
 
cout prints data to the screen.
Example: cout << "Hello world";

Another example:
Code:
#include <iostream.h>
struct POINT
{
	int X;
	int Y;
}
void main()
{
	POINT mPoint;		// Declare a point
	POINT* pPoint = NULL;	// Declare a pointer to a POINT

	pPoint = &mPoint;		// Get the address from mPoint and copy it to pPoint
	
	mPoint.X = 12;		// mPoint.X is now 12
	
	pPoint->Y = 17;		// mPoint.Y is now 17
	(*pPoint).Y = 17;		// Same thing as above, other notation

	cout << mPoint.X;		// Prints 12 on the screen
	cout << pPoint->Y;		// Prints 17 on the screen
}
 
ohhh ok thanks alot! but how do you know that pPoint is pointing to in a big program?
 
Rafa 5.0 said:
Stupid Java doesn't even load in an emachine
and what does that have to do with the pointer discussion ? :)
 
Back
Top