Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

There are always alternatives. -- Spock, "The Galileo Seven", stardate 2822.3


devel / comp.lang.java.programmer / switch is shared declaration

SubjectAuthor
* switch is shared declaratione.d.pro...@gmail.com
+* Re: switch is shared declarationArne Vajhøj
|+- Re: switch is shared declarationArne Vajhøj
|`* Re: switch is shared declaratione.d.pro...@gmail.com
| `- Re: switch is shared declarationMichael Jung
`* Re: switch is shared declarationMarcel Mueller
 `* Re: switch is shared declarationEric Sosman
  +* Re: switch is shared declarationMarcel Mueller
  |`- Re: switch is shared declarationArne Vajhøj
  `- Re: switch is shared declarationArne Vajhøj

1
switch is shared declaration

<af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10010&group=comp.lang.java.programmer#10010

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:ac8:5f92:0:b0:3f3:89cf:7f5f with SMTP id j18-20020ac85f92000000b003f389cf7f5fmr5741290qta.13.1684930543443;
Wed, 24 May 2023 05:15:43 -0700 (PDT)
X-Received: by 2002:a81:b147:0:b0:54c:2409:c306 with SMTP id
p68-20020a81b147000000b0054c2409c306mr10152574ywh.6.1684930543172; Wed, 24
May 2023 05:15:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Wed, 24 May 2023 05:15:42 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=174.178.2.211; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 174.178.2.211
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
Subject: switch is shared declaration
From: e.d.prog...@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Wed, 24 May 2023 12:15:43 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 16
 by: e.d.pro...@gmail.com - Wed, 24 May 2023 12:15 UTC

I don't use a lot of switches so I hadn't realized this but:
this is invalid:
if (1==1) {
String s;
} else {
s = "1";
}

but this is fine:
switch (1) {
case -1:
String s;
break;
case 1:
s = "1";
break;
}

Re: switch is shared declaration

<u4lfmo$311sp$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10011&group=comp.lang.java.programmer#10011

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arn...@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Wed, 24 May 2023 12:57:59 -0400
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <u4lfmo$311sp$1@dont-email.me>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 24 May 2023 16:58:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c39f57dca40f54b1529be7aef7fd9cfe";
logging-data="3180441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/I7/4DlFku4OYM1nR/qlpKvAsD67fvuGs="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:tcaPadiZwMr/GiQ0CxGFUi2ULMs=
Content-Language: en-US
In-Reply-To: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
 by: Arne Vajhøj - Wed, 24 May 2023 16:57 UTC

On 5/24/2023 8:15 AM, e.d.pro...@gmail.com wrote:
> I don't use a lot of switches so I hadn't realized this but:
> this is invalid:
> if (1==1) {
> String s;
> } else {
> s = "1";
> }
>
> but this is fine:
> switch (1) {
> case -1:
> String s;
> break;
> case 1:
> s = "1";
> break;
> }

The entire switch sort of has to be a single scope due to
fall through (no break) being valid.

Arne

Re: switch is shared declaration

<u4lgpe$3186o$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10012&group=comp.lang.java.programmer#10012

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arn...@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Wed, 24 May 2023 13:16:28 -0400
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <u4lgpe$3186o$1@dont-email.me>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4lfmo$311sp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 24 May 2023 17:16:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c39f57dca40f54b1529be7aef7fd9cfe";
logging-data="3186904"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3jtlNmuwHlQHi7hEHUzvw4Ax6vFi1fW8="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:DueOiOFxyxAijsP6NFz6VlglzHg=
In-Reply-To: <u4lfmo$311sp$1@dont-email.me>
Content-Language: en-US
 by: Arne Vajhøj - Wed, 24 May 2023 17:16 UTC

On 5/24/2023 12:57 PM, Arne Vajhøj wrote:
> On 5/24/2023 8:15 AM, e.d.pro...@gmail.com wrote:
>> I don't use a lot of switches so I hadn't realized this but:
>> this is invalid:
>> if (1==1) {
>>    String s;
>> } else {
>>    s = "1";
>> }
>>
>> but this is fine:
>> switch (1) {
>> case -1:
>>    String s;
>>    break;
>> case 1:
>>    s = "1";
>>    break;
>> }
>
> The entire switch sort of has to be a single scope due to
> fall through (no break) being valid.

Difficult to come up with a good example where
the variable is always proper initialized.

But:

public class Fallthrough {
public static void main(String[] args) {
switch(3) {
case 3:
int z;
z = 1;
case 2:
z = 2;
case 1:
z = 3;
System.out.println(z);
}
}
}

Arne

Re: switch is shared declaration

<72906bb1-301c-4729-be5b-e037a71286b9n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10013&group=comp.lang.java.programmer#10013

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:a05:620a:40d2:b0:75b:3b20:51a2 with SMTP id g18-20020a05620a40d200b0075b3b2051a2mr730833qko.6.1684951526976;
Wed, 24 May 2023 11:05:26 -0700 (PDT)
X-Received: by 2002:a81:e508:0:b0:552:b607:634b with SMTP id
s8-20020a81e508000000b00552b607634bmr11650700ywl.4.1684951526669; Wed, 24 May
2023 11:05:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.us.feeder.erje.net!feeder.erje.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Wed, 24 May 2023 11:05:26 -0700 (PDT)
In-Reply-To: <u4lfmo$311sp$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=174.178.2.211; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 174.178.2.211
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com> <u4lfmo$311sp$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <72906bb1-301c-4729-be5b-e037a71286b9n@googlegroups.com>
Subject: Re: switch is shared declaration
From: e.d.prog...@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Wed, 24 May 2023 18:05:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 27
 by: e.d.pro...@gmail.com - Wed, 24 May 2023 18:05 UTC

> > I don't use a lot of switches so I hadn't realized this but:
> > this is invalid:
> > if (1==1) {
> > String s;
> > } else {
> > s = "1";
> > }
> >
> > but this is fine:
> > switch (1) {
> > case -1:
> > String s;
> > break;
> > case 1:
> > s = "1";
> > break;
> > }
> The entire switch sort of has to be a single scope due to
> fall through (no break) being valid.
>
> Arne

I guess it makes sense. You could call it a soft scope? In the if statement you get an undeclared variable error as it knows the if block will never execute with the else condition. In the switch statement it also will never execute both the declaration and the assignment, unless you remove the break. Since the break it optional, the compile sees it as one, though it also executes fine.

Re: switch is shared declaration

<87jzws7mfh.fsf@golem.phantasia.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10014&group=comp.lang.java.programmer#10014

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!.POSTED!not-for-mail
From: mij...@golem.phantasia.org (Michael Jung)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 12:57:54 +0200
Message-ID: <87jzws7mfh.fsf@golem.phantasia.org>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4lfmo$311sp$1@dont-email.me>
<72906bb1-301c-4729-be5b-e037a71286b9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: eJwljUEKAjEMAM/6ipJ760FhD6WwXwm11EibBJPi912VuQ4z3N6WmNVviZvnoGJO3CPWKou9wKTn2vWB7GiESV4d8vk0pPevdkfHAtt2PYAcJtKIVaYOJHaLLgWWtaO883/k+htBuIQPnVwqvw==
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux)
Cancel-Lock: sha1:jU7EhKRe5Va9z4Tcnr2nrtgf5SU= sha1:qDPNSQuv6ICvHZYyqi7laOFQ/8I=
X-Abuse-Contact: "https://abuse.nntp4.net"
 by: Michael Jung - Sun, 28 May 2023 10:57 UTC

"e.d.pro...@gmail.com" <e.d.programmer@gmail.com> writes:
>> > I don't use a lot of switches so I hadn't realized this but:
>> > this is invalid:
>> > if (1==1) {
>> > String s;
>> > } else {
>> > s = "1";
>> > }
>> > but this is fine:
>> > switch (1) {
>> > case -1:
>> > String s;
>> > break;
>> > case 1:
>> > s = "1";
>> > break;
>> > }
>> The entire switch sort of has to be a single scope due to
>> fall through (no break) being valid.
>> Arne
> I guess it makes sense. You could call it a soft scope? In the if
> statement you get an undeclared variable error as it knows the if
> block will never execute with the else condition. In the switch
> statement it also will never execute both the declaration and the
> assignment, unless you remove the break. Since the break it optional,
> the compile sees it as one, though it also executes fine.

I wouldn't call it anything scope. Remember that you can create a lot of
mischief with labels and continue and breaks. And that needs to remain
compatible. And just because the compiler knows (can know) a block
cannot be reached, does not allow you to add illegal, but parseable
statements, e.g. "else { 0 = 1; }".

Re: switch is shared declaration

<u4ve27$3t8pn$1@gwaiyur.mb-net.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10015&group=comp.lang.java.programmer#10015

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.2a02:908:1b12:f7a0:a00:27ff:fe8d:1f8d!not-for-mail
From: news.5.m...@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 13:31:19 +0200
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <u4ve27$3t8pn$1@gwaiyur.mb-net.net>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 28 May 2023 11:31:19 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net; posting-host="2a02:908:1b12:f7a0:a00:27ff:fe8d:1f8d";
logging-data="4105015"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:S23Fl8Mz0/mlF9y4Q4qPWtZsr+w=
X-User-ID: U2FsdGVkX190pvQwaTvzY6pvxHUGsJ5fFG7c7qG0De8p0l6229UdSA==
In-Reply-To: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
Content-Language: de-DE, en-US
 by: Marcel Mueller - Sun, 28 May 2023 11:31 UTC

Am 24.05.23 um 14:15 schrieb e.d.pro...@gmail.com:
> but this is fine:
> switch (1) {
> case -1:
> String s;
> break;
> case 1:
> s = "1";
> break;
> }

Shouldn't this be a problem too because the 2nd switch label bypasses
the initialization of s?

Marcel

Re: switch is shared declaration

<u4vhsg$sea0$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10016&group=comp.lang.java.programmer#10016

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: esos...@comcast-dot-net.invalid (Eric Sosman)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 08:36:33 -0400
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <u4vhsg$sea0$1@dont-email.me>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4ve27$3t8pn$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 May 2023 12:36:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2f773b9c245c58da34c04ac488c725bd";
logging-data="932160"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2jzQZv4iaJwXAn89fhly/"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.2
Cancel-Lock: sha1:/j5caiLwaSpkXedxK8yXImGQU1E=
In-Reply-To: <u4ve27$3t8pn$1@gwaiyur.mb-net.net>
Content-Language: en-US
X-Antivirus: AVG (VPS 230528-6, 5/28/2023), Outbound message
X-Antivirus-Status: Clean
 by: Eric Sosman - Sun, 28 May 2023 12:36 UTC

On 5/28/2023 7:31 AM, Marcel Mueller wrote:
> Am 24.05.23 um 14:15 schrieb e.d.pro...@gmail.com:
>> but this is fine:
>> switch (1) {
>> case -1:
>>    String s;
>>    break;
>> case 1:
>>    s = "1";
>>    break;
>> }
>
> Shouldn't this be a problem too because the 2nd switch label bypasses
> the initialization of s?

The `case 1:' part *is* the initialization of `s'. There is a
*declaration* of `s' in the `case -1:' part, but the declaration
does nothing (here) to initialize the `s' reference.

If we added another case to the switch, say

default:
System.out.println(s);
break;

.... the compiler would complain that `s' might be used without
being initialized, true. But the code as shown does not try
to retrieve any value from `s', so the compiler holds its peace.

There's really no mystery here at all. What e.d.programmer has
observed is simply that the block governed by `switch' is just
like any other block: A declaration within a block has scope
that runs from the declaration to the block end. The fact that
this block is part of a `switch' does not change matters: a
block is a block is a block.

--
esosman@comcast-dot-net.invalid
Look on my code, ye Hackers, and guffaw!

Re: switch is shared declaration

<u4vk7b$3t8pn$8@gwaiyur.mb-net.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10017&group=comp.lang.java.programmer#10017

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.2a02:908:1b12:f7a0:a00:27ff:fe8d:1f8d!not-for-mail
From: news.5.m...@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 15:16:27 +0200
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <u4vk7b$3t8pn$8@gwaiyur.mb-net.net>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4ve27$3t8pn$1@gwaiyur.mb-net.net> <u4vhsg$sea0$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 May 2023 13:16:27 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net; posting-host="2a02:908:1b12:f7a0:a00:27ff:fe8d:1f8d";
logging-data="4105015"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:p3vjMKNP+Jhw+2J8XJxKk3sux5I=
In-Reply-To: <u4vhsg$sea0$1@dont-email.me>
Content-Language: de-DE, en-US
X-User-ID: U2FsdGVkX1+0rEirutVWK47okUjbbkVOb4qJ77KOHPajX4fIknLRew==
 by: Marcel Mueller - Sun, 28 May 2023 13:16 UTC

Am 28.05.23 um 14:36 schrieb Eric Sosman:
> On 5/28/2023 7:31 AM, Marcel Mueller wrote:
>> Shouldn't this be a problem too because the 2nd switch label bypasses
>> the initialization of s?
>
> The `case 1:' part *is* the initialization of `s'.  There is a
> *declaration* of `s' in the `case -1:' part, but the declaration
> does nothing (here) to initialize the `s' reference.
>
> If we added another case to the switch, say
>
>     default:
>         System.out.println(s);
>         break;
>
> ... the compiler would complain that `s' might be used without
> being initialized, true.   But the code as shown does not try
> to retrieve any value from `s', so the compiler holds its peace.

So ist is only valid because of the optimization?

String s;
is a default constructor call.

s = "1";
is an assignment operator call.
I expected the latter to be invalid without the first (unless String is
a trivial type).

> There's really no mystery here at all. What e.d.programmer has
> observed is simply that the block governed by `switch' is just
> like any other block: A declaration within a block has scope
> that runs from the declaration to the block end.

I know. Switch labels are nothing else than goto targets.

Marcel

Re: switch is shared declaration

<u503q0$va0v$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10018&group=comp.lang.java.programmer#10018

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arn...@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 13:42:22 -0400
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <u503q0$va0v$1@dont-email.me>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4ve27$3t8pn$1@gwaiyur.mb-net.net> <u4vhsg$sea0$1@dont-email.me>
<u4vk7b$3t8pn$8@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 May 2023 17:42:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b44133095db4622383e5373aef64abf2";
logging-data="1026079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SXu25C11dMYBnneDlBaV2H8F0vbmfEw8="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.1
Cancel-Lock: sha1:9Bo3ZjvHc0z0nV719zwkQp9rDLI=
In-Reply-To: <u4vk7b$3t8pn$8@gwaiyur.mb-net.net>
Content-Language: en-US
 by: Arne Vajhøj - Sun, 28 May 2023 17:42 UTC

On 5/28/2023 9:16 AM, Marcel Mueller wrote:
> Am 28.05.23 um 14:36 schrieb Eric Sosman:
>> On 5/28/2023 7:31 AM, Marcel Mueller wrote:
>>> Shouldn't this be a problem too because the 2nd switch label bypasses
>>> the initialization of s?
>>
>> The `case 1:' part *is* the initialization of `s'.  There is a
>> *declaration* of `s' in the `case -1:' part, but the declaration
>> does nothing (here) to initialize the `s' reference.
>>
>> If we added another case to the switch, say
>>
>>      default:
>>          System.out.println(s);
>>          break;
>>
>> ... the compiler would complain that `s' might be used without
>> being initialized, true.   But the code as shown does not try
>> to retrieve any value from `s', so the compiler holds its peace.
>
> So ist is only valid because of the optimization?

Not a traditional optimization.

The check if things used are guaranteed to be initialized
just do not apply.

>   String s;
> is a default constructor call.

No. s is just a reference not pointing to any object.

>   s = "1";
> is an assignment operator call.

An assignment.

> I expected the latter to be invalid without the first (unless String is
> a trivial type).

You can not assign to a variable not declared no matter whether
it is simple or not. But I suspect that was not what you meant.

String s;
s = "1";

is sort of equivalent to C:

char *s;
s = "1";

Arne

Re: switch is shared declaration

<u503sq$va0v$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10019&group=comp.lang.java.programmer#10019

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arn...@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: switch is shared declaration
Date: Sun, 28 May 2023 13:43:52 -0400
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <u503sq$va0v$2@dont-email.me>
References: <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com>
<u4ve27$3t8pn$1@gwaiyur.mb-net.net> <u4vhsg$sea0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 28 May 2023 17:43:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b44133095db4622383e5373aef64abf2";
logging-data="1026079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jWa1eNCcqgJ+BbKhWQmw6uubUNg9m8v8="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.1
Cancel-Lock: sha1:uaorLHAgxHVbPKvRlfU6YB5oZ+4=
In-Reply-To: <u4vhsg$sea0$1@dont-email.me>
Content-Language: en-US
 by: Arne Vajhøj - Sun, 28 May 2023 17:43 UTC

On 5/28/2023 8:36 AM, Eric Sosman wrote:
>> Am 24.05.23 um 14:15 schrieb e.d.pro...@gmail.com:
>>> but this is fine:
>>> switch (1) {
>>> case -1:
>>>    String s;
>>>    break;
>>> case 1:
>>>    s = "1";
>>>    break;
>>> }

> There's really no mystery here at all. What e.d.programmer has
> observed is simply that the block governed by `switch' is just
> like any other block: A declaration within a block has scope
> that runs from the declaration to the block end.  The fact that
> this block is part of a `switch' does not change matters: a
> block is a block is a block.

and case ... break; is not a block.

:-)

Arne

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor