Is Java a good language to learn Besides C++ ??

Pages: 1... 345
@SouleDancer: Regarding your example of passing a pointer by value:
SouleDancer wrote:
If you pass a reference to a variable like f( &in ), you can change "in", but you cannot change the passed value (a manual reference) so that it is remembered when the function returns.
Why would you want to modify the temporary? :P

Regarding you saying pass by reference is bad: Most C++ implementations do references via hidden pointers. This usually ends up with them passing the hidden pointer by value, just as in your example.
1
2
3
4
5
6
class Blah
{
    char &ref;
public:
    Blah(char &c) : ref(c) {}
};
1
2
cout << sizeof(char &) << endl;
cout << sizeof(Blah) << endl;
http://ideone.com/fS88h

I have something to ask you (unrelated to above) or anyone who knows. I am trying to learn the "Java way" of things, so this may seem stupid.

In Java, you only have mutable and immutable types, but that is not defined by any language feature - it is defined only by the choice of whether to allow change through methods. What about an ArrayList? How can I guarantee that the original is not changed without making an expensive copy or deep copy?

This is a 'this language has and this language doesn't have' question. C++ has const-correctness without expensive copy. Java has cross-your-fingers-or-make-expensive-copies.

Don't tell me to extend ArrayList and override the methods that change it-what if I need to do this for a final class? What if one of its elements is modified by the reference returned from get()?

My question: Does Java support optional const-correctness without copying? And how?

Other than const correctness I'm perfectly fine with everything in Java (it took me a while to say that though). Still learning though, and remember I come from C++ on this view of Java.
@Gaminic: Yes sure it is a good usage of references but you can easily achieve that with pointers and the point was to find the reasons of why c++ brought references other than syntaxic sugar.
Last edited on
Wha...what!? WHAT!? I thought references WERE syntactic sugar... <:O
LB are you joking?If not I'm not sure but i think it's little more than syntaxic sugar. How do you acheive this without references:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string addletter(std::string const& str, char l)
{
std::string ret = str;
ret.append(1, 'a');
return ret;
}

std::string& addletter(std::string& str, char l)
{
return str.append(1, l);
}

int main()
{
cout << addletter("ttt", 'u') << endl;
std::string s = "ttt";
cout << addletter(s, 'u') << endl;
}

without references you probably have to work with pointers, new and delete to obtain the same result without leaks.
Pointers yes, new and delete no. See my post above... http://ideone.com/fS88h ...References are syntactic surgar for constant pointers :)
Or when used like this v they are optimized to be different names for the same thing:
1
2
int a;
int &b (a); //a gets an additional name: b 
Last edited on
closed account (z05DSL3A)
L B wrote:
References are syntactic surgar for constant pointers
err no, not unless there is so much sugar that it has crushed the pointer out of any recognisable shape.

(or you have an odd understanding of syntactic sugar)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a(int &v)
{
    ++(v);
}
int b(int *const v)
{
    ++(*v);
}

//...

int x = 1;
a(x);
b(&x);
It seems pretty recognizeable...
A reference is basically just moving the ampersand to a different location. I think about the only difference between a pointer and a reference is that you know that the reference was actually referencing valid data the moment it was passed.
@Grey Wolf, you have an odd understanding of syntactic sugar.

http://en.wikipedia.org/wiki/Syntactic_sugar wrote:
Specifically, a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do: functionality and expressive power will remain the same.


I've been through this thread before ( http://www.cplusplus.com/forum/beginner/58499/ ), so I'm not sure why I'm getting into it again. That thread ended (as far as I'm concerned) with Moschops pointing out that references can be used to extend the lifetime of temporary a object. Luckily the term "syntactic sugar" invalidates this argument (as equivalent behaviour can be achieved with plain assignment, thanks to RVO).

I'll assume that we all have at least intuitive understanding of logic. If someone wants to prove that references are not syntactic sugar, he should
1. Claim that wikipedia has the wrong definition, and present a better one.
or
2. Prove that references do provide functionality that cannot be achieved without them by presenting an example of such case. I'm hoping that the vague definition of expressive power will not be abused.
Last edited on
closed account (z05DSL3A)

1)
syntactic sugar definition

Term coined by Peter Landin for additions to the syntax of a language which do not affect its expressiveness but make it "sweeter" for humans to use. Syntactic sugar gives the programmer an alternative way of coding that is more succinct or more like some familiar notation. It does not affect the expressiveness of the formalism.
Syntactic sugar can be easily translated ("desugared") to produce a program in some simpler "core" syntax. E.g. C's "a[i]" notation is syntactic sugar for "*(a + i)".
http://dictionary.reference.com/browse/syntactic+sugar

2, I would rather ask a question. Is the addition of a type, to a language, an addition to the syntax of the language to make it 'sweeter' for humans to use? I personally don't see how you can be using different types and say that one is syntactic sugar for the other.
Last edited on
1. I don't see what that definition changes. Could you explain? To expand on "sweetness",
Stroustrup wrote:
References are useful for several things, but the direct reason I introduced them in C++ was to support operator overloading.
http://www2.research.att.com/~bs/bs_faq2.html#pointers-and-references
Was it "easily" that you found problematic? I guess that would make sense, although it's a bit vague.

2. As for adding a type, possibly it hasn't been done anywhere else, but it does not conflict with either definition, so why should it be a problem?
Aren't ints just syntactic sugar for bit[32]?
closed account (z05DSL3A)
With regard to the definition, I had not actually clocked the link to the wikipedia article and was just going by the snippet you posted. I just posted a fuller definition.

As to point 2, if you want to think that adding a type and the syntax associated with that type while not changing the syntax of an already existing type is Syntactic sugar you go ahead.
@Gaminic, you can't add ints without using int type. Though if inline asm was in the standard, such claim would be arguably valid.
I don't see why you prefer intuitive definition over a formal one. It makes life complicated.
Aren't ints just syntactic sugar for bit[32]?


The integer datatypes provide semantics that do not exist in bit[something], so no.
Pointers yes, new and delete no

Without new and delete and references I have the first overload returning by value and the second overload returning a pointer. Plus I have to declare the constant to use the first overload because you can't take adresses of rvalues:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string addletter(std::string const* str, char l)
{
	string s = str->c_str();
	s.append(1, l);
	return s.c_str();
}

string* addletter(string* str, char l)
{
	return &str->append(1, l);
}

int main()
{
	const std::string cstr = "ttt";
	cout << addletter(&cstr, 'u').c_str() << endl;
	std::string s = "ttt";
	cout << addletter(&s, 'u')->c_str() << endl;
}

So without references(in particular const references) it's much harder to work with temporaries, that's why i find it is more than syntaxic sugar.
Last edited on
What in the world are you talking about? Why can't you use const pointers?
Are you familiar with lvalues and rvalues? Basically(the real definition is more complex) an lvalue is something you can take the adress. Temporaries are rvalues: you can't take their adress, this code is illegal(fortunately):
 
addletter(&string("ttt"), 'u');

WIthout references you just can't handle temporaries properly unless I've missed something, in case i'm curious on how to write my example without references and the ability to call addletter with a temporary.
Last edited on
Works here:
1
2
3
4
5
6
7
8
9
10
11
void func(string const *const s)
{
	cout << *s << endl;
}

typedef const string constring;

int main()
{
	func(&(constring("hello")));
}


See http://ideone.com/lHKB6
Last edited on
Your code doesn't compile in VC2010 in debug (but compile in release) because you take the adress of a temporary and this is very dangerous. Sorry but i'm tired of this debate as you don't answer my questions and send me other stuff so i will let you meditate on this code which I hope will prove you the importances of const references when working with temporaries:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>

using namespace std;

string const* toptr(string const& str)
{
	return &str;
}

string const* mul(string const* str)
{
	return toptr(*str + *str);
}

void print(string const* str)
{
	cout << *str << endl;
}

int main()
{
	print(mul(toptr("test")));
}

This is undefined behaviour
Now, this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string const& toptr(string const& ptr)
{
	return ptr;
}

string mul(string const& str)
{
	return toptr(str + str);
}

void print(string const& str)
{
	cout << str << endl;
}

int main()
{
	print(toptr(mul("test")));
}

will work as const& will keep the temporary alive.
Topic archived. No new replies allowed.
Pages: 1... 345