Categories
askquestion

I want more syntactic sugar in my Java! [closed]

I want more syntactic sugar in my Java! [closed]

Ask Question

Asked
11 years, 1 month ago

Active
10 months ago

Viewed
4k times

.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}

7

2

It’s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.

Closed 7 years ago.

Syntactic sugar for properties for example in C#:

private int x;

public int X{
get { return x; }
set { x = value; }
}

or simply

public int X{ get; set; }

I am missing verbatim strings in java… @”C:My Documents” instead of “C:\My Documents\”

Do you agree Java needs more “sugar”? Any one knows is there is sugar coming in next Java versions?

java syntactic-sugar

share|improve this question

edited Dec 19 ’08 at 20:02

João

asked Dec 19 ’08 at 18:08

JoãoJoão

1,88955 gold badges1717 silver badges2424 bronze badges

3

Asking for information on the direction of the language is not argumentative, nor is it subjective. Parts of the post are subjective, but not all of it. Reopened.

– tvanfosson
Dec 19 ’08 at 18:21

I agree Java needs to move forward, not only syntactic sugar, but new concepts as well. Nowadays, it’s loads more fun to code in current C# than in current Java.

– Vinko Vrsalovic
Dec 19 ’08 at 18:23

Actually, the posted code won’t work. You need to either get rid of the private field, (3.0 style) or put the return and assignment in explicitly.

– Chris Cudmore
Dec 19 ’08 at 18:54

@chris: What happen if you use: public int X{ get; } ?? My guess is the following won’t compile: object.x = 1; ?

– OscarRyz
Dec 19 ’08 at 20:33

3

This isn’t a real question imho.

– cletus
Dec 19 ’08 at 21:11

 | 
show 1 more comment

15 Answers
15

active

oldest

votes

12

While I don’t necessarily agree with Java’s philosophy, I think that adding lots of syntactic sugar to Java would go against its philosophy. Java is supposed to be a very simple, easy to reason about language with few constructs, kind of a lowest common denominator lingua franca in the programming community. It was largely a reaction to the complexity of C++. There is supposed to be very little “magic” in it, beyond what is needed (garbage collection, mostly) to make it a memory-safe language.

If you want more sugar, etc. in a reasonably high-performance, statically typed language, I would say that Java is just the wrong language for you. You’re probably better off just using C#, or maybe D.

share|improve this answer

edited Mar 28 ’19 at 5:57

Hearen

4,77322 gold badges2424 silver badges3737 bronze badges

answered Dec 19 ’08 at 18:30

dsimchadsimcha

61.4k4141 gold badges187187 silver badges313313 bronze badges

1

“Java is supposed to be a very simple”, agreed, but there are things in Java that can get messy… why not add “sugar” to make things more enjoyable?

– João
Dec 19 ’08 at 20:29

1

because as soon as more sugar is added, unintentional complexity ensues =)

– Chii
Dec 20 ’08 at 12:40

or use Scala or Kotlin for lots of concise syntax, since they also run on the JVM and inter-operates with Java.

– voxoid
Apr 4 ’18 at 19:21

add a comment
 | 

16

“Syntactic sugar causes cancer of the semicolon.”

— Alan Perlis. Epigrams on Programming.

share|improve this answer

answered Dec 19 ’08 at 18:37

Diomidis SpinellisDiomidis Spinellis

16.7k22 gold badges4444 silver badges7272 bronze badges

3

Funny but not a real answer. Should I downvote this?… mmhhh I’m tempted… mmmmhh… Naahh I think I would save 1+ of my rep 😛

– OscarRyz
Dec 19 ’08 at 21:38

Thanks! Re: “real answer”, others have answered this question quite well.

– Diomidis Spinellis
Dec 20 ’08 at 11:36

add a comment
 | 

14

Sounds like you want Groovy… Apparently properties are on their way, although not in Java 7 (as @erickson helpfully corrected me about in a comment).

Groovy does have nice string sugar.

share|improve this answer

edited Dec 19 ’08 at 19:02

answered Dec 19 ’08 at 18:23

Dan VintonDan Vinton

23k88 gold badges3434 silver badges7979 bronze badges

Yes! Java should integrate some of Groovy’s features.

– Vinko Vrsalovic
Dec 19 ’08 at 18:26

Remi Forax and others are working on properties, but it’s definitely not a done deal for Java 7. JavaFX properties should help them along though.

– erickson
Dec 19 ’08 at 18:29

I’m not positive they are out, but I think they are unlikely. Joe Darcy is leading the effort to collect “small” language changes for Java 7, implying that there’s not enough time left to settle “large” changes. Check out Alex Miller’s Java 7 language clearinghouse: tech.puredanger.com/java7

– erickson
Dec 19 ’08 at 19:43

Agree. Groovy is good for that and lot more.

– OscarRyz
Dec 19 ’08 at 20:36

add a comment
 | 

6

I’ve also developed in both Java and C# the last few years, and find C# a superior language with regards to expressiveness and powerful language constructs. The Java language does not undergo the same degree of changes and updates as C#, atleast not at the same pace. I still don’t necessarily mean that Java should be drastically updated, but we need a powerful and expressive statically typed language on the Java platform. I think Scala is going to develop into this replacement language, which we Java developers can switch to when ordinary Java does not cut it.

C# is an absolutely fantastic language; probably the “best” statically typed language these days, but Java is still in my opinion a superior platform. I like Java the platform, and I like C# the language.

share|improve this answer

answered Dec 19 ’08 at 18:43

Bent André SolheimBent André Solheim

1,04177 silver badges1010 bronze badges

+1 because: I like Java the platform, and I like C# the language.

– Camilo Martin
May 19 ’12 at 1:54

1

+1 for mentioning Scala

– gpilotino
Feb 6 ’13 at 16:57

add a comment
 | 

4

As per Mark Reinhold’s talk at Devoxx 2008, property support will not be added to Java in Java 7.

http://hamletdarcy.blogspot.com/2008/12/java-7-update-from-mark-reinhold-at.html

More info on properties in Java 7 ideas here:

http://tech.puredanger.com/java7#property

share|improve this answer

answered Dec 19 ’08 at 21:55

Alex MillerAlex Miller

61.3k2626 gold badges105105 silver badges157157 bronze badges

add a comment
 | 

3

While properties are nice, they are not java. I seriously think the javabean spec closed that door ages ago. I think there are clearer cases for syntactic sugar that is needed:

Use of inner classes due to lack of delegates/closures. Current syntax is from hell.
Methods as first-order language constructs, ie method data types.
Type inference with generics
Run-time presence of generics in reflection API.
Events

share|improve this answer

answered Dec 19 ’08 at 19:54

krosenvoldkrosenvold

66.6k2626 gold badges134134 silver badges199199 bronze badges

add a comment
 | 

3

If I use java for anything large again, it will be as an output language from another compiler.

share|improve this answer

answered Dec 19 ’08 at 20:16

JoshuaJoshua

28.4k55 gold badges5151 silver badges111111 bronze badges

add a comment
 | 

2

That is oldschool C#. Nowadays it is

public int X { get; set; }

share|improve this answer

answered Dec 19 ’08 at 18:10

Matt BriggsMatt Briggs

36.1k1212 gold badges8383 silver badges124124 bronze badges

Sorry, I don’t code in C# for quiet some time. This is just two things I miss from the old days at school, since Microsoft practically kicked Java out of all the school curriculum…. but that’s a different story any way…

– João
Dec 19 ’08 at 18:15

Microsoft hasn’t even come close to equaling java in University curriculum. I know very, very few major universities that use C# honestly.

– mmcdole
Dec 19 ’08 at 18:24

1

What about major universities that use C# dishonestly? 🙂

– GalacticCowboy
Dec 19 ’08 at 18:29

In Portugal all major Universities know have strong connections to Microsoft…

– João
Dec 19 ’08 at 18:33

It was not “old days @ school” but ” C# old school ” read ( first C# programmers). I don’t quite get this anyways. If X is public, the get/set are kind of already there… why don’t just use it : public int X; and call object.x = 1; int a = object.x;

– OscarRyz
Dec 19 ’08 at 20:31

 | 
show 5 more comments

2

Sugar is bad for your teeth. Syntactic sugar is bad for your brain.

share|improve this answer

answered Dec 19 ’08 at 18:16

Paul TomblinPaul Tomblin

156k5454 gold badges289289 silver badges384384 bronze badges

2

but sweet for my eyes and… fingers…

– João
Dec 19 ’08 at 18:18

Hear hear. Assembly language forever.

– mackenir
Dec 19 ’08 at 18:18

No, it’s good. Automatic properties, for example, mean that when reading code you can see more on the screen at a time and your brain spends less time doing mundane parsing and more time evaluating the meaning of the code.

– Joel Coehoorn
Dec 19 ’08 at 18:18

“Syntactic sugar” can enhance the terseness (tersitude? tersosity?) of the language, which in turn is actually a good thing for your brain.

– GalacticCowboy
Dec 19 ’08 at 18:31

1

“syntactic sugar causes cancer of the semicolon” — Alan Perlis

– bendin
Dec 19 ’08 at 20:14

 | 
show 1 more comment

1

This isn’t entirely necessary.

A simple editor macro could work:

prop int x –>

private int x;

public int getX(){
return x;
}
public void setX(int val){
x = val;
}

Edit: (in response to comments)

How is this any less readable than:

private int x;

public int X{
get { return x; }
set { x = value; }
}

share|improve this answer

edited Dec 19 ’08 at 18:52

answered Dec 19 ’08 at 18:27

Chris CudmoreChris Cudmore

26.6k1212 gold badges5050 silver badges9191 bronze badges

yes, in terms of writing it is true… in terms of reading… I am not so sure…

– João
Dec 19 ’08 at 18:31

Agreed. Code is read many more times than it is written.

– Bent André Solheim
Dec 19 ’08 at 18:45

1

In your first example the property is written with two separated methods, in the second there is only one construct, the property, composed by a setter and a getter. More the second can be written in a even more simplified to read way like Matt Briggs pointed out.

– João
Dec 19 ’08 at 19:38

why don’t you just make the attribute public? That’s far more readable, simpler ( and awful ) public int x; // that’s it!

– OscarRyz
Dec 19 ’08 at 20:26

public fields causes a leak in the implementation.

– Chii
Dec 20 ’08 at 12:40

add a comment
 | 

1

I’ve written some annotations (and an annotation processor) that helps this quite a bit.

See http://code.google.com/p/javadude/wiki/Annotations

For example:

@Bean(
properties={
@Property(name=”name”, bound=true), // String is default type
@Property(name=”age”, type=int.class, bound=true)
}
)
public class Foo extends FooGen {}

This generates FooGen containing the fields and get/set methods, as well as making them bound (which is optional). There are many other fun things you can do with these as well.

NOTE: I’m doing a few tweaks now that deprecate the various “override” options.

Enjoy,

— Scott

share|improve this answer

answered Dec 31 ’08 at 22:57

Scott StanchfieldScott Stanchfield

26.6k99 gold badges4343 silver badges5858 bronze badges

add a comment
 | 

0

One thing Java would do really well to implement is something equivalent to ref and out parameters.

share|improve this answer

answered Dec 19 ’08 at 18:49

Chris CudmoreChris Cudmore

26.6k1212 gold badges5050 silver badges9191 bronze badges

add a comment
 | 

0

I don’t like “syntactic sugar” as is mainly because it would be yet another thing to learn and would most likely get abused eventually. I have cursed at the annoyance of making getters and setters myself though so I understand why one would want to make creating those as easy as possible but I’d rather see @Get, @Set and @GetSet annotations than more syntax thingamajiggers to do the job.

share|improve this answer

answered Dec 19 ’08 at 20:28

EskoEsko

26.5k1010 gold badges4949 silver badges7777 bronze badges

Seem to me a nice idea, Get/Setter annotation could do the trick… as long it lets you change the getter or setter behavior down the line without breaking the object interface.

– João
Dec 19 ’08 at 20:41

2

It’s not “another thing to learn”, because you have to learn the “setVar”/”getVar” pattern anyway.

– niXar
Dec 24 ’08 at 23:43

add a comment
 | 

0

The java platform has adopted a huge amount of programming languages. If you want more syntactic sugar you could use another language like Groovy or (J)Ruby and it will still run on the jvm and work with your other java-libraries. (I even think there is a C# implementation 😉

share|improve this answer

answered Dec 19 ’08 at 22:00

finpingvinfinpingvin

64477 silver badges1313 bronze badges

add a comment
 | 

0

As with many, i am stuck to manage application written in Java 1.4 (migration to 1.5 is one the way in many cases). So even if Java 7 or 8 get new feature I will not be able to used them… Anyways syntactic sugar is good, it can help the writing, reading and analysis of code.

share|improve this answer

edited Dec 20 ’08 at 1:13

answered Dec 19 ’08 at 20:19

JoãoJoão

1,88955 gold badges1717 silver badges2424 bronze badges

add a comment
 | 

Not the answer you’re looking for? Browse other questions tagged java syntactic-sugar or ask your own question.

Blog

Podcast: Make my Monolith a Micro

When laziness is efficient: Make the most of your command line

Featured on Meta

TLS 1.0 and TLS 1.1 removal for Stack Exchange services

Did Stack Exchange cut the number of negative comments nearly in half between…

An account of my meeting with the Stack Overflow management team

Visit chat

Related

6375Is Java “pass-by-reference” or “pass-by-value”?3197How do I efficiently iterate over each entry in a Java Map?1462How does the Java ‘for each’ loop work?3093What is the difference between public, protected, package-private and private in Java?3945How do I read / convert an InputStream into a String in Java?3044When to use LinkedList over ArrayList in Java?3423How do I generate random integers within a specific range in Java?493Java multiline string2940How do I convert a String to an int in Java?3149How to create a memory leak in Java?

Hot Network Questions

Why don’t organs have weighted keys?

When was Alice Born?

How can a Z80 assembly program find out its own memory address?

Believable way to make missiles and drones inferior to analog starfighters?

Did Voldemort know that his Muggle father was actually a victim?

Why isn’t the center of the Earth cold?

Which is easier to develop: a piston prop engine or a jet engine?

Did Lucille Ball have a creative influence on Star Trek?

Using two tires on one wheel to prevent punctures?

Group Anagrams (C#)

What prevents me from setting up an A name entry for “google.ca” to whatever IP I want?

What should I (a professor) do if a student keeps answering other students’ questions before I start explaining?

Article of Abbreviations

How can I constructively address the fact that a coworker with the same (senior) title doesn’t have the basic skills for the role

Did Nancy Pelosi say “We’ll have to pass the bill to find out what’s in it”?

What are the official opposing schools of magic?

Is there any benefit to partioning hdd data-only disk?

Double deck vs wide body airliner, why would anyone build a double deck one?

I have a 2nd broken spoke after 300km on a new bike, is it the wheel or me?

Is there a hot key that automatically makes a group of vertices into a circle?

What purpose are the transistors in this diagram serving?

Why is CMB an evidence of hotter and denser early Universe?

How do I avoid a temporary short circuit from destroying the PI?

At what point does an aircraft become an airliner?

more hot questions

lang-java

Leave a Reply

Your email address will not be published. Required fields are marked *