+ 5

Why are Object Oriented Languages like java so...strict about some things unlike js,ruby e.t.c

In java,c#,c++ e.t.c you have to specify every data type. In java,unless an arraylist is used you cannot push or insert elements into arrays or even remove them once its size is declared. Arrays cannot have elements of different data types Please this is not a debate saying some OOP is better than some OOP.im just saying some languages like js,ruby dont take this things too seriously. Ive never endulged in real world programming but i dont know how this java's behavior is advantageous . enlighten me!

9th May 2018, 5:48 PM
᠌᠌Code X
᠌᠌Code X - avatar
7 Antworten
+ 6
Java is like a king who never goes against his words Example : you declare an array called "arr" int []arr = new int(5) here, you have said that it only contains int type of values.. So adding a value of a different type is like going against your words!
9th May 2018, 6:01 PM
Dlite
Dlite - avatar
+ 2
languages like js have typed values. true, this gives the progeammer some freedom and has it's advantages too. but it can cause confusion and complications too. because of this nature of the language, especially in big and complicated programs, you might do operations with different types of values, which can cause coersion/convertişng one type to another and the progam may behave in a way you didn't expect. and because you're doing nothing wrong the compiler/interpreter will not show an error. on the other hand, languages like c++ are very strict. variables must be declared with their types. sounds annoying but it saves you from unintended coersion. if you accidentially operate with two different types of variables you get an error. you know exactly where the problem is. then there is the issue with performance/speed.
9th May 2018, 6:10 PM
storm
storm - avatar
+ 2
I think JavaScript has types just like c++ and c# but these types are hidden from the programmer , the compiler takes care internally of converting types .
14th May 2018, 7:47 AM
Rabee Abbas
Rabee Abbas - avatar
+ 1
You can still have arrays of multiple data types, using unions. As for why you can't add elements to arrays, it's because they're statically allocated arrays (sequential chunks of memory, while in languages like python, linked lists are used more, which links objects that can exist at any point in memory). In C++ for example, you can use std::vector for dynamically allocated arrays, and std::list for linked lists. I'm sure similar things exist for Java as well, but I'm just unaware of them. As for them using a static type system, instead of a dynamic type system, (aka an integer is always an integer), this is useful in many cases, especially for low level programming. Having a dynamic type system means storing information about the type and constantly evaluating it. This takes up both memory and processing power. In Java/C#/C/C++ all that is handled at compile time. A 32-bit integer takes up 4 bytes, and is referred to by that address. During run-time the computer can't tell what is at those 4 bytes (whether it's a floating point number or integer or something else), but the compiler made sure that it will be always treated like an integer. This stuff requires a basic understanding of assembly to figure out, but once you have that, it's not really that complicated. TL;DR: * Python arrays aren't arrays, they're liked lists, and they exist in lower-level languages too. * We use a static type system for performance reasons. As a footnote, you don't have to specify every data type. In C++, for instance you can write auto x = 4 or auto x = 5.2 and it works just fine.
9th May 2018, 6:31 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
storm C++ isn't strong, it's weak. Even weaker then python. So weak you can pass the underlying binary representation of an object from one type to another in just one line of code: auto new_value = *((new_type*) ((void*) old_value)) ; Oh yeah, and you don't have to tell it the type. You can just use auto. It's not strong, it's ___static___. And it's static for performance reasons. EDIT: Made the binary copy code shorter.
9th May 2018, 6:39 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
Vlad Serbu i don't remember calling it "strong". just pointing out the differences regarding variables. as the original question/opinion was in that regard too.
9th May 2018, 6:59 PM
storm
storm - avatar
+ 1
storm Oh yeah, you said strict, and I misread that as strong. Sorry about that then.
9th May 2018, 7:09 PM
Vlad Serbu
Vlad Serbu - avatar