Why do Java and C++ have syntactical similarities? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I noticed that C++ and Java seem to have many features in common in terms of syntax, although I would assume them to be fairly different languages. For example, both languages support casting and define functions by specifying the return type and then the name of the function. The for loops also seem very similar. Also, they both use curly braces for a variety of statements and structures.
Is there a reason for this? Did Java and C++ have some common predecessor, or was the syntax of one based on the other, for example? Or am I just reading too much into this?

Java is just a member of the family of C-style languages (i.e. the languages syntactically based on C Programming Language). The family includes languages like
C++, Java, JavaScript, PHP, Perl
which were very popular, for example, in 1990-s and the beginning of 2000-s.
Today's popularity of
Python, Ruby, Rust, Kotlin, Swift and others.
slightly moved the focus away from C-style languages. One of the reasons to move away from C-style is that C has context-dependent grammar and the new languages tend to have almost context-independent grammar, which makes its parsing easier and more predictable.

Anyone designing a new language would have nothing to gain by using different syntax for the same operations. So if the potential users of the language are, for example, used to "a++" being a variable "a" whose value increments but returns the existing value of a, or they're used to braces being what delimits a block of code, there's no reason for a new language not to use those same things. If this new language did switch these things, up, they risk potentially confusing and discouraging people from using it.
This extend not just to Java and C++, but some other languages as well. For example, JavaScript and Perl use many of the same syntax features that Java and C++ do.

Related

Why does Java Boolean implement Comparable? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
In Java, operators <, >, >= and <= are not defined for the primitive boolean type.
However, the corresponding wrapper class Boolean implements Comparable.
That is: true > false
is an error, but Boolean.TRUE.compareTo(Boolean.FALSE) > 0 is fine.
How come?
Did the language designers change their mind?
Why keep the incoherent behavior, then?
Although arbitrary, I can think of advantages to having a total order defined for booleans.
Are there any disadvantages?
Programming languages are not mathematical constructs. They are complex projects spanning many years and a lot of different people. As such, they are subjected to opinions, legacy, disagreements, hype cycles, influences of other languages, poor communication, and unfortunately sometimes also to mistakes and stupidity. You could argue that most decisions about a language are in fact arbitrary.
Your question is perfectly valid: why is it like this? Unfortunately without asking people who made the relevant commits how much they can still remember is not really a viable option. So your guess is as good as anybody else's.
It is what it is, but you are entitled to have your own opinion. Sadly, such inconsistencies can be in some cases frustrating to the point when people abandon a language and create a new one. But since computers are physical, limited things, any new language will also be imperfect and opinionated.
If you ask me, having a total ordering on boolean is a good idea - it wouldn't hurt anybody, while it could provide some limited benefit in certain (although very narrow) cases. But there are many more, much much bigger issues with Java. As it stands, I don't think Oracle will risk breaking any existing programs by changing this behaviour.

coding style :curly bracket after if statement [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
which coding style is better and what are the types of people in image programmers will know
which is better as a good programmer?
First writing curly braces after if statements - or curly braces on new line after if statement?
if(condition){
}
if(condition)
{
}
Although this is very much a matter of style, the "Java way" of doing it has always been to put the opening brace on the same line as the statement it is attached to. So
if(condition) {
}
is the suggested way of doing it. Who suggests this? Well, Sun did (and now Oracle maintains that page, too). And Google does.
I'd say that it's up to you to decide, which one suits you best. Also, from what I know, some languages are encouraging to use specific style:
Java preffers camelCase and
if (true) {
}
C# PascalCase and
if (true)
{
}
However, it's important to keep style consistent in project. If you mix styles in single file / project, that's bad practice.
As other has written, there are certain standards that promote
if (whatever) {
}
as the "better" solution.
In that sense, "better" actually means:
the majority of Java programmers is used to this style
therefore, for the majority of programmers, it will be easier to read code that is following the common styles
or vice versa: if you deviate from the common standards, everybody reading your source code will burn unnecessary brain cpu cycles to accustom to that deviating style
(and keep in mind: normally, source code is written "once"; but read maybe hundreds or thousands of time. thus "wasting brain cpu" is a serious issue)
But, on the other hand, the answer is: it depends.
My personal experience is: if you are following a consistent style, then the details of that style do not matter too much. You see, if your company has its own style conventions; and everybody is following those ... then you have to adapt. Period. And honestly: it is not that hard. I was asked to use all kinds of style conventions. And you simply adapt. It might take some time, but in the end, you will have no problems reading/writing code that maybe says
if (whatever)
{
So, when your team says: for our own good reasons, everybody has to use that second style, then that's it.
But if you are working on open source, or you are the person defining the style to use - then simply stay close to the "official" conventions from Oracle.
And final note: code formatting is a tooling question. You make sure that you specify your style convention so that your IDE can enforce it. Then you are even free to use your very personal style - just reformat source upon checkout; and turn back into "other peoples style" prior checkin to you source code repository.

Binding, glue code and wrapper library [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I many times saw terms binding, glue, and wrapper in a similar context and meaning(if we are talking about some "adapter code"). So what exactly means each of these terms? Is it a synonyms? Or what is the difference?
binding, wrapper and glue are - in this context specifically - are all sort of synonyms of each other and can sort of be used interchangeably..
bind, wrap and glue are all verbs which mean they're a word to describe an action, state or occurrence; even more-so they all revolve contextually around attachment. So yes - they are synonyms as far as the english language is concerned. However, concerning programming and the written paradigms used to describe ones actions within it may become more 'grey'.
Let us use an example. Directx and c#:
binding by definition is to "tie or fasten" something to something else, as is glue and to a lesser (but still valid) extent wrap. Which makes sense in this example as dx is is written and designed for c++, but there are libraries that wrap the c++ code providing a useable library for c# programmers to use; inside which, the code binds or glues the original c++ code to the c# equivalent - and vice versa - allowing these two (originally incompatible) sources to communicate.
So in using the c# alternative allows us access to the - if fully featured - functionality of the original c++ version.
So when talking about a wrapper one can refer to the base as binding or glue code. However - in my opinion - wrapper is a much broader term contextually, of which contains the actions of binding and 'gluing'.
This is my understanding - if somebody believes me mistaken, please don't hesitate to correct me.

Translator: pseudo-code to a concrete language (PHP, JAVA) library suggestion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm building a visual programming tool and was wondering if you could suggest a library or even throw at me ideas about a tool that I can build myself that translates the pseudo-code to a concrete language as PHP, JAVA. For example for a pseudocode like that:
do i = 1 to 100
print "Hello World !!!";
newline;
end do
I would like to get something similar to:
in PHP:
for ($i = 1; $x <= 100; $x++) {
echo "Hello World !!!";
echo PHP_EOL;
}
in Java:
for(int i = 1; x <= 100; x++) {
System.out.print("Hello World !!!");
System.out.print("\n");
}
I don't just want a translator from PHP to Java or vise versa.
I guess my question is: Will I be able to generate code in all kinds of languages after specifying some basic language structure rules and writing the pseudo-code? I want to break the gap between the pseudocode and code in a concrete language so I won't need to translate the code by hand. Also by writing the algorithm in pseudocode once, I would generate the code in a whatever language I want.
Thank you for your feedback!
edit: ok, so not "pseudo" code then, defining a language that will work as a intermediate. Does any one have a suggested library for that I can look into?
The whole point of pseudo-code is that it has no definite syntax and no definite semantics. It is a way for human beings to communicate algorithms to other human beings in a way that avoids the reader and writer having to worry about such details.
That means that it impossible to translate automatically. Instead, you need a human being to understand what the pseudo-code means and then write code in a concrete programming language that has the same effect.
Another way to look at this is that if "pseudo-code" had defined syntax and semantics, it wouldn't be "pseudo". It would be real code.
Of course, there is nothing stopping you from defining a language of this nature ... but then you'd need to implement compilers and/or translators for your pseudo-pseudo-code ( :-) ).
OK, so not "pseudo" code then, defining a language that will work as a intermediate ...
There are various tools that you could use to help you build a source code to source code translator. Search for "parser generator" and "template engine".

java: libraries for immutable functional-style data structures [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
This is very similar to another question (Functional Data Structures in Java) but the answers there are not particularly useful.
I need to use immutable versions of the standard Java collections (e.g. HashMap / TreeMap / ArrayList / LinkedList / HashSet / TreeSet). By "immutable" I mean immutable in the functional sense (e.g. purely functional data structures), where updating operations on the data structure do not change the original data, but instead return a new instance of the same kind of data structure. Also typically new and old instances of the data structure will share immutable data to be efficient in time and space.
From what I can tell my options include:
Functional Java
Scala
Clojure
but I'm not sure whether any of these are particularly appealing to me. I have a few requirements/desirements:
the collections in question should be usable directly in Java (with the appropriate libraries in the classpath). FJ would work for me; I'm not sure if I can use Scala's or Clojure's data structures in Java w/o having to use the compilers/interpreters from those languages and w/o having to write Scala or Clojure code.
Core operations on lists/maps/sets should be possible w/o having to create function objects with confusing syntaxes (FJ looks slightly iffy)
They should be efficient in time and space. I'm looking for a library which ideally has done some performance testing. FJ's TreeMap is based on a red-black tree, not sure how that rates.
Documentation / tutorials should be good enough so someone can get started quickly using the data structures. FJ fails on that front.
Any suggestions?
It seems to me you already know what your options are, you just aren't happy with any of them. Here is my take on the three choices you've provided:
Functional Java - This one seems like the best fit for you. It fits all of your requirements except that you don't like the documentation. From my perspective the documentation looks basic, but serviceable. Their code snippets should get you up and running quickly. The learning curve seems almost non-existent which should help mitigate the lack of documentation. FYI, core Java's TreeMap is based on a Red-Black tree as well.
Scala - This is the choice I would make if I was in your shoes. You seem to not want to learn a new language, but Scala is a very easy transition from Java. You can write very java-like code at first, and slowly adopt more functional idioms. The Java <-> Scala interop is excellent in both directions as well.
Clojure - As much as I love Clojure, its tough to recommend in this particular instance due to the radically different syntax and steep learning curve for a java developer.
Perhaps Google's guava-libraries may be of some use:
https://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained
Scala
You can call the methods of either language from methods in the other one
http://www.scala-lang.org/faq/4
I've spent some time making the Clojure persistent/immutable data-structures work in Java, with sensible constructors and generics as part of Pure4J.
This also includes #ImmutableValue class for ensuring that not only are the collections immutable, but the values you put in them are immutable too.
Hope this helps.

Categories

Resources