[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

is this what people always say when they talk about how painful

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 3

File: java-bg-672x372.jpg (19KB, 672x372px) Image search: [Google]
java-bg-672x372.jpg
19KB, 672x372px
is this what people always say when they talk about how painful java is to use?

I have this function (and yes I know it doesn't work right):
public static int[] integerBubbleSort(int[] list){    
for(int i = 0; i < list.length - 1; i++){
if(list[i] > list[i + 1]){
list = switchpos(i, i + 1, list);
}//else: implicitly if list[i] <= list[i + 1], do nothing.
}
return list;
}

which is called later:
int[] list = new int[]{70,5,10,40,20,3,7};
System.out.println(Arrays.toString(integerBubbleSort(list)));
System.out.println("Sorted from original array: " + Arrays.toString(list));


Printing the array after it's gone through the function is what I expect, but then the "sorted from orignal array x" is not at all what I expect; that being it's the same both times.

So I guess what's going on here is that, in the bubble sort function, there's the line assigning list to something, which means that Java sees that variable as the same one of the same name I define later.
Like what the fuck, like, that just seems so messy and weird. Why can't the function just be closed? In math if you define a function in terms of x, that doesn't fucking mean x is always defined in that way, it's called a FUCKING VARIABLE for a reason.

I don't want to write functions with side-effects, how do I make that clear to the compiler?
>>
>bubble sort
You must be 18 to post here.
>>
>>57356151
19 2bh
I am indeed concerned that I am too far behind in skillz but everyone else in my intro to CS is far behind me so whatever.
>>
http://www.java-made-easy.com/variable-scope.html

unless I'm reading this wrong though, list in the bubble sort method should already be in method scope. So why is it affecting the list declared in main?
Or is something else going on?

Just to clarify both print statements have outputs where 70 is at the end.
>>
>>57356132
>bubblesort
java doesn't have references for basic types, so everything that isn't an object you can't mutate in a function. so either you use some cancer Integer type and wrap the int around it, or you do the swap in the if scope
>>
>>57356487
>so everything that isn't an object you can't mutate in a function
Well it seems like that's something I would want here, but it seems like that is happening?
>>
>>57356132
>can't write a simple program
>blames the language
>creates post on /g/
>"is this what people always say when they talk about how painful java is to use?"
You must be +18 to post on this website.
>>
>>57356827
I fucking said in the OP I know the thing doesn't work. This isn't a conceptual thing it's a technical thing.
>>
File: dgi.jpg (30KB, 598x363px) Image search: [Google]
dgi.jpg
30KB, 598x363px
>>57356132
Now unless I'm more retarded than you (not likely), you need to do the bubbling N times, fuckwit.

Has nothing to do with Java meme.
>>
youre modifying the original array you nigger
if you want immutability, you have to copy the array first and then modify the copy
>>
>>57356132

Well, every langauge has different design decisions. In Java, you pass an reference to an object (i.e. Arrays), so you are changing the actual object.

If you want two objects - well, create a second array then:

public static int[] integerBubbleSort(int[] list){

int[] list_tmp = new int[list.length];
System.arraycopy(list, 0, list_tmp, 0, list.length);

for(int i = 0; i < list_tmp.length - 1; i++){
if(list_tmp[i] > list_tmp[i + 1]){
list_tmp = switchpos(i, i + 1, list_tmp);
}//else: implicitly if list_tmp[i] <= list_tmp[i + 1], do nothing.
}
return list_tmp;
}



I didn't checked if it complies or not, but it's somehow like that..
>>
>>57356132
Learn what "pass by reference" and "pass by value" are. This kind of shit is exactly why people should start with C or even assembler, not those garbage collected garbage languages.
>>
>>57356974
OP is just mad that non primitive types are implicitly pass by reference rather than by value.
>>
>>57356974
jesus christ. refer to >>57356900.

>>57357039
>>57357118
ok, thanks I get it now.
I don't like it though.
>>
>>57357165

>I don't like it though.

But it makes sense.

Imagine you have really big objects and want to do pass it to some other method. If you would create a duplicate every time your performance would really suffer. Also why would someone duplicate objects? Most or the time you create something, change it, return it and that's it. cloning objects is a sort of special case.


Also, one suggestion:
How about printing "that's the original array:", then sorting it and then printing "..and that's the sorted version:"
>>
File: sicp.jpg (172KB, 1280x720px) Image search: [Google]
sicp.jpg
172KB, 1280x720px
>>57357165
>I don't like it though.
It might be confusing at first. People who don't have experience working directly with pointers or who don't understand the call stack get confused. I started programming with C++ and that helped me out in this regard.

E.g. If I call this method: someFunction(somePrimitive, someObject)
And I change the value of somePrimitive, those changes won't occur in the calling method... If I change the properties of someObject, those changes will occur in the calling method. If I change the actual reference of someObject in the called method (by directly reassigning someObject,) that change will not occur in the caller because the reference will remain the same there. Why? Because in the called method, you're working with the value of the reference in the new stack frame. (This is also why your changes to somePrimitive will not occur in the caller.)

Once you get this down, it's not very difficult, but it confuses beginners.

>>57357118
>In Java, you pass an reference to an object (i.e. Arrays), so you are changing the actual object.
This is correct, but some people will read this and think Java is pass by reference. Java is pass by value, always. The references themselves are passed by value.

Note that, good practice in an OO language (which is frequently ignored) is to avoid changing your arguments in the called method. Arguments are supposed to be inputs to a function/method. Return values are supposed to be the output of the function. When you are changing your arguments (and depending on those changes in the caller,) you are essentially using arguments as output (counter-intuitive!) This is covered in Clean Code. That said, there is a cost in making a deep copy, so ignoring this makes sense some of the time...
Thread posts: 16
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.