![]() |
|
[Java] Equivalent of C#-fixed-keyword for Java - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Equivalent of C#-fixed-keyword for Java (/showthread.php?tid=6086175) |
Equivalent of C#-fixed-keyword for Java - Nemo2478 - Jun 9, 2020 Hello everybody, what is the equivalent of the fixed-keyword of C# for Java? In other words, how can I translate this code: Code fixed (Char* sPtr = s) { //s=Any String variableto Java? Regards and thanks RE: Equivalent of C#-fixed-keyword for Java - Aussie - Jun 11, 2020 I don't actually think there is an equivalent in Java. Having read the C# definition, "the fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement", the thing is Java doesn't actually make use of pointers. Could you explain a use case for this in Java? RE: Equivalent of C#-fixed-keyword for Java - Vanagander - Jun 11, 2020 The fixed keyword prevents the garbage collector from relocating the data to another memory adress. So if you have a pointer to something, the GC can move whatever the pointer was pointing to before you need to use it and when you do need it it’s been moved(if not fixed). Java doesn’t use pointers like C# so afaik there is no equivalent. RE: Equivalent of C#-fixed-keyword for Java - DyMa - Jun 12, 2020 Java has no memory management like others have said and fixed in C# is bad practice and unsafe shouldn't be used and I can't see why you would need to RE: Equivalent of C#-fixed-keyword for Java - Nemo2478 - Jun 12, 2020 Hello everyone, thank you for your answers. I believed that using Pointers you could convert a String to in Char implicitly, as you can see in the example. But I think that is enough in Java: Code return converttoFg(sPtr, s.Length); //Any methodRegards and thank you |