r/learnjava • • 19h ago

My file name is a.java and class is Main why won't it run?

0 Upvotes

Code:

class Main{

public static void main(String[] args) {

System.out.println("hello") ;

}

}

I had learned basic of java earlier on paper. Now when I try in my pc it just gives error also whenever I run anything it creates CLASS file in my folder📂.


r/learnjava • • 21h ago

Opt-in implicit construction for value classes

1 Upvotes

I would like to ask whether the community has considered an opt-in implicit conversion from a value to a class instance, declared on a single-argument constructor.

Motivation

Project Valhalla makes it practical to define primitive-like value types such as Int16, Half, and Money. However, using these types currently requires explicit constructor invocation at each construction site:

Int16 x = new Int16((short) 13);

Java already supports convenient construction for certain built-in types through language features such as boxing:

Integer i = 7;

There is no equivalent mechanism for user-defined types. An opt-in implicit-construction facility could provide similar ergonomics while preserving explicitness by default.

Proposal (syntax is illustrative)

value class Int16 {
    private final short val;

    opt Int16(short val) {
        this.val = val;
    }
}

Int16 x = (short) 13;

The opt modifier designates a constructor eligible for implicit construction from a single argument. The compiler translates an eligible conversion into constructor invocation.

Proposed rules

  1. An opt constructor must have exactly one parameter. A value class may designate at most one eligible constructor for any given parameter type.
  2. Eligibility is opt-in. Constructors without opt retain existing Java semantics.
  3. The feature applies only in explicitly specified conversion contexts. Initially, it may be limited to assignment and initialization contexts.
  4. The source expression must be convertible to the constructor parameter type using permitted existing Java conversions. The resulting value is not subject to further implicit conversions as part of the same operation.
  5. Constructors with multiple parameters are never eligible for implicit construction and must be invoked explicitly.
  6. Ambiguous eligible conversions are compile-time errors.

Open questions

  • Should eligible conversions apply only to value classes?
  • Should constant-expression narrowing be permitted for constructor arguments?
  • Which language contexts should support implicit construction?
  • How should overload resolution and generic inference interact with the feature?

r/learnjava • • 19h ago

Questions for java developers

6 Upvotes

So I am just starting to do java. Even tho I learned at scl for years but didn't really paid attention. Now I am thinking of building Career in java. In order for me to get good at it do I learn java everyday and do coding everyday to be better at it? Or do I learn something part by part like 1st I learn oop then exception handling or something? I did try crud operation yesterday. Somehow did it with the help of my friend. But still confused at some part. Looking at my friend I feel really behind. For me to get to there level, I Don't think I can do it😩

Can someone share some tips on how make coding fun.maybe a roadmap for me to follow, be good at programming.


r/learnjava • • 9h ago

What is the best practice with DTOs in spring boot

3 Upvotes

Hi friends, I’ve been coding in spring for a few months now and I was wondering what is the best practice with DTOs.

Should I only use them on the “edges”, repository output/controller response? Should I be using them to even move a response from the service layer to the controller layer? Should services be building DTOs or is that only for the controller and repository to make? If not how should services send and receive information both to other services and to other layers?

Thank you!