Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Explain the different ways available for the initialisation of string variable?

+1 vote
asked Nov 23, 2023 by suchithra s poojary (130 points)
Explain the different ways available for the initialization of string variable?

2 Answers

0 votes
answered Nov 24, 2023 by Yonas Fayera (180 points)

We can initialize in 4 ways:

1 When we declare the variable we can initialize it at that moment.

String name = "Yonas";

2. After the Declaration of the variable we can initialize the variable.

    String name;

    name = "Yonas"

3. Assing the value of another variable:

       String name = "Yonas"

       String Second = name

4. Assign variable from user input

  Scanner input= new Scanner(System.in)

     String abc =input.nextline()

0 votes
answered Nov 24, 2023 by Peter Minarik (86,240 points)
edited Dec 6, 2023 by Peter Minarik

What language are we talking about?

Most language supports assignment at declaration, e.g.:

mystring = "This is an example"
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...